- Home /
Instantiated rooms don't function properly.
Hi, so my game features rooms that instantiate after the player opens a door. I got the player to open the door and have the following room instantiate just fine. The problem is that the script that spawns more rooms no longer works within the instantiated new room, so when I open a door, nothing happens.
I suspect that because I'm telling it to look for a value in a script within a gameObject as a trigger, it's reading that as true since the previous door was opened. And it finds the status of the script of the previous use of that script in the first prefab and not in its own local one. I also suspect this is due to naming conventions I used, since the only change in the names of the instantiated room is "Name of Prefab (Clone)". I don't know how to detect this via a function instead of just hardcoding it like I have here.
using UnityEngine;
using System.Collections;
public class RoomSpawnL : MonoBehaviour
{
public Transform Room_Prefab;
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 int RoomNum = 10;
// Use this for initialization
void Start ()
{
Debug.Log("START --> " + this);
DoorOpen = GameObject.Find ("Door_L"); //Unity looks for a GameObject called "Door_R" in my hierarchy and makes the Door_R variable equal that.
}
// Update is called once per frame
void Update ()
{
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*>()
// for (int i = 0; i < RoomNum; i++) {
if (door.open == true) {
if(!door.spawnOnce) {
door.spawnOnce = true;
Instantiate (Room_Prefab, transform.position, transform.localRotation);
Debug.Log ("New room!");
}
} else if (door.opening == false) {
Debug.Log ("No new room... :( ");
}
//}
}
}
I'm not very sure what exactly I should be doing here to fix this problem. Do I make an array of naming conventions? Is there method or function I can use to manage this?
Answer by aldonaletto · Oct 11, 2013 at 01:18 AM
From your previous question, the idea is to instantiate a new room when the door of the previous room is opened. In order to achieve this, you should somehow link an exit door to its room. The easiest way would be to make the exit door a child of the room prefab, and find it with transform.Find (GameObject.Find will return the first "Door_L" it founds in the scene, which may not be the one you want). Supposing that Door_L is a child of the room prefab, the code could be something like this:
public class RoomSpawnL : MonoBehaviour {
public Transform Room_Prefab;
OpenableDoor door; // door is the exit door's OpenableDoor script
public int RoomNum = 10;
void Start (){
Transform exitDoor = transform.Find("Door_L"); // find the Door_L childed to this room
door = exitDoor.GetComponent<OpenableDoor> (); // get the exit door script
}
void Update (){
if (door.open && !door.spawnOnce) { // if door opened and next room doesn't exist yet...
// create the new room:
Instantiate (Room_Prefab, transform.position, transform.localRotation);
door.spawnOnce = true; // make sure it's created only once
}
}
}
If you want to enumerate the rooms, save a reference to the new room and assign the number:
...
// create the new room:
Transform newRoom = Instantiate (Room_Prefab, transform.position, transform.localRotation) as Transform;
// assign the next number to it:
newRoom.GetComponent<RoomSpawnL>().RoomNum = RoomNum + 1;
...
With that code, now the room instantiation doesn't work at all...
Just adding the Update portion of your code only also has no effect.
Your answer
![](https://koobas.hobune.stream/wayback/20220613115720im_/https://answers.unity.com/themes/thub/images/avi.jpg)