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 11, 2013 at 12:45 AM · instantiateprefabnameactivation

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?

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 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;
      ...
Comment
Add comment · Show 1 · 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 11, 2013 at 09:02 PM 0
Share

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

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

16 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

Related Questions

Naming Instantiated Prefabs 2 Answers

Issues with tracking prefab instances and gameobject naming (JS) 1 Answer

Assigning a prefab a number suffix at runtime. 1 Answer

[C#] How can I destroy instantiated prefabs when many are created with the same name? 2 Answers

Game Object seems empty after instantiation 1 Answer


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