Home Unity

Instantiated Rooms don't work properly

SuperSparkplug
polycounter lvl 9
Offline / Send Message
SuperSparkplug polycounter lvl 9
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 GameObject variable for my door
        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_L 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*>() 
            
            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?

Replies

  • kio
    Offline / Send Message
    kio polycounter lvl 16
    first off - your comments do more harm than help

    float speed = 20 // set speed to 20;

    well that is kinda obvious isnt it? the best code is code who doesnt even need comments...

    to your problem:

    GameObject.Find("Door_L") <- returns the first gameobject it finds with the name, in your case it seems to find the one where you flagged spawnOnce to true, and nothing happens after that.

    Instead of searching the GameObject you could use soemthing like this
    using UnityEngine;
    using System.Collections;
    
    public class RoomSpawnL  : MonoBehaviour
    {
        // assign these in the inspector
        public GameObject Room_Prefab;
        public OpenableDoor connectedDoor;
    	
    	
        void Update ()
        { 
            if (door.open == true && !door.spawnOnce) 
    	{
    	      door.spawnOnce = true;
    	      Instantiate (Room_Prefab, transform.position, transform.localRotation);   
            }
        }
    
    }
    

    On a higher level - what is the motivation to spawn the rooms when opening the doors?
  • SuperSparkplug
    Offline / Send Message
    SuperSparkplug polycounter lvl 9
    Obvious to you, maybe, but not me, who is not a natural programmer. Plus, I sometimes leave those comments in so that, months down the line, if I need to return to it for some reason, I can quickly figure things out.

    Made that change and there's no difference.

    The player is supposed to be asked a question and open one of 3 doors representing their answer. Each time they open a door, a room spawns on the other side of it. I can make a single room appear after opening a door. However, opening a door in this new room does not create another new room, even though they are running off the same prefab.
  • WarrenM
    You're saying that if you returned to this code months later:

    "float speed = 20 // set speed to 20;"

    You wouldn't be able to figure out what it's doing without the comment?
  • SuperSparkplug
    Offline / Send Message
    SuperSparkplug polycounter lvl 9
    WarrenM wrote: »
    You're saying that if you returned to this code months later:

    "float speed = 20 // set speed to 20;"

    You wouldn't be able to figure out what it's doing without the comment?

    ...Try looking at my code. Did you see that there? No? You have your answer.
  • WarrenM
    OK. How about this line then?

    GameObject DoorOpen; //Create a GameObject variable for my door

    I double checked and that line IS in your code. I'm simply reinforcing the point that Kio was trying to make -- obvious comments actually make code harder to read, not easier.
  • SuperSparkplug
    Offline / Send Message
    SuperSparkplug polycounter lvl 9
    ...

    Anyway, I restarted Unity to see if that would change anything and it did. Now the instantiated rooms do spawn more rooms! :)

    The only problem remaining is that besides the first room instantiated, all new rooms have the wrong rotation orientation, it seemingly seems set to 0,0,0. The way I set the position and rotation for each room is by putting an invisible object near a door and making the instantiation go by its transform and rotation. While the transform is correct on all instantiations, the rotation isn't. Why is that?
  • Parkar
    Offline / Send Message
    Parkar polycounter lvl 18
    You are using the world space location but the local rotation when you instantiate the new room.

    The short answer is that you probably just need to use transform.rotation instead of transform.localRotation.

    The reason it matters is probably because your RoomSpawnL objects parent is another room and that rooms rotation is not 0,0,0.
Sign In or Register to comment.