- Home /
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;
}
}
}
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
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.
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.
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
Follow this Question
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