Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SuperSparkplug · Oct 10, 2013 at 05:20 PM · instantiateprefabinfinite

How do I stop infinite instantiation?

My game features a character opening doors and a new room instantitates on the other side of the door when its opened. I've been able to achieve that, but the problem is that the door instantiates infinitely and I can't seem to stop it. How do I only instantiate it once?

Here's some code I'm using.

 using UnityEngine;
 using System.Collections;
 
 public class RoomSpawnL : MonoBehaviour {
     
     public Transform Room_Prefab;
     //private Vector3 OriPos;
     //private Vector3 OriRot;
     //GameObject Door_R = GameObject.Find("Door_R");
     GameObject DoorOpen; //Create a Game Object variable called Door_R
     OpenableDoor door; //Door is an OpenableDoor variable type based on the existing OpenableDoor script?
     public bool spawnOnce = false;
     
     //public GameObject Room_Prefab_Reference; 
     //Room_Prefab myPrefab;
     
     // Use this for initialization
     void Start () {
         
         
     }
     
     // Update is called once per frame
     void Update () {
         
         DoorOpen = GameObject.Find("Door_L"); //Unity looks for a GameObject called "Door_R" in my hierarchy and makes the Door_R variable equal that.
         
         door = DoorOpen.GetComponent<OpenableDoor>(); //Door Variable = *Name of the GameObject in the hierarchy or the GameObject/Variable with that data*.GetComponent<*Script on said object I want to access*>() 
         
         if(door.open == true){
             if(!spawnOnce) {
                 Instantiate (Room_Prefab, transform.position, transform.localRotation);
                 Debug.Log("New room!");
                 spawnOnce = true;
             }
         }
         else if (door.opening == false) {
         
             Debug.Log("No new room... :( ");
             spawnOnce = false;        
             }
     
         }
 
 
                     
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by deltamish · Oct 10, 2013 at 05:24 PM

Hi try altering these lines as show below

From

  if(door.open == true){
 if(!spawnOnce) {
 Instantiate (Room_Prefab, transform.position, transform.localRotation);
 Debug.Log("New room!");
 spawnOnce = true;
 }

To

  if(door.open == true){
 if(!spawnOnce) {
 spawnOnce = true;
 Instantiate (Room_Prefab, transform.position, transform.localRotation);
 Debug.Log("New room!");
 
 }

and please do check if **door.open** remains true at all times

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SuperSparkplug · Oct 10, 2013 at 07:37 PM 0
Share

I tried the code. We're getting closer. :)

It stops the infinite instantiation... But now it spawns 2 rooms, with the second one partially intersecting with the first. Any idea why?

Also, how is what you did any different? It seems that you just switched the lines around.

avatar image MarkD · Oct 10, 2013 at 07:55 PM 0
Share

Well is the scene empty before instantiating? otherwise you need to do a checkup on the location of the current room and adjust the position of your instantiate based on that.

you could also do a checkup whether a room already exist with gameObject.find and if so simply put the spwnOnce on True, so your instantiate simply won't start.

avatar image SuperSparkplug · Oct 10, 2013 at 08:12 PM 0
Share

No, there is an room environment I've modeled with 3 doors at the start. You press "E" when near them to open. When opened, this script uses a cube game object as a point of reference to instantiate based on its position and rotation if the door is opened based off the open boolean in another script. Here's the where the open variable is called in the Opening Door script, which the above script is accessing

     void Update () {
                 if(open){
             //Open door
             //if (!opening) {
             //nextRoom = Instantiate(Room_Prefab);
             //nextRoom.transform.position = Vector3(nextRoom.transform.position.x + 25, 0, 0);
             //Instantiate(Room_Prefab);
             transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * smooth);
             open = true;
             //opening = true;
             //}
             
         }
         
         else{
             //Close door
             transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * smooth);
             //opening = false;
         }
 

As you can see there is an if statement and another variable boolean commented out. The reason being is that originally they stopped my room from infinitely instantiating when I put the instantiation in the door's script. Originally, I planned to use it, ins$$anonymous$$d of open as the variable to help toggle the instantiation, however, whenever the opening boolean is called, my door simply twitches slightly ins$$anonymous$$d of actually opening and I have no idea why. Why is that and could this variable better help solve my problem?

A for your second point, what exactly would I ask for in GameObject.Find since it's will eventially be instantiating further clones of the room. Each room is supposed to instantiate a new room. Wouldn't that cause confusion for that function?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Scrolling level Instantiate after cycle 0 Answers

Lobbing a Bomb with Instantiate. 1 Answer

Randomly pick then create prefab 2 Answers

Associate objects to a prefab 1 Answer

instantiating vertically 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges