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;
}
}
}