Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Vitally · Aug 04, 2015 at 09:46 PM · unity 5instantiateprefabarray of gameobjects

Array values changing after gameObject is instantiated from prefab

Hello,

For the past week my and my father have been struggling with a problem during the recursive creation of a procedural game map (2D, from a set of tiles that make up components of the map). We have a piece of code that selects a weighted random room from an array and then calles a method to create that room.

(Simplified)

 public GameObject[] prefabSections;
 public int[] prefabProbabilities;
 
 if (rm.CanCreateNewRoom())
 {
     for(int i = 0; i < prefabSections.Length; i++)
     {
         if(prefabProbabilities[i] > UnityEngine.Random.Range (0, 100))
         {
             SpawnNewRoom(prefabSections[i]);
             break;
         }
     }
     if (!isOcupied)
     {
         SpawnNewRoom(prefabSections[0]);
     }
 }

where SpawnNewRoom(); instantiates the room tile randomly selected

(Simplified)

         GameObject newRoom = Instantiate (prefabRoom, newRoomPosition, newRoomRotation) as GameObject;
         newRoom.transform.parent = transform.parent.parent;
         newRoom.name = "Room" + (roomNumber++).ToString ("D3");
         Debug.Log (transform.parent.name + " Created " + newRoom.name + " With Node " + name);

witch all works great!

the problem comes in the next generation of rooms created. the rooms instantiated by SpawnNewRoom(); public GameObject[] prefabSections array has been changed to include a reference to the gameobject that instantiated it instead of the array that's on the prefab! somehow the act of Instantiating the new rooms changes the array even though at no point in the code is that array changed. How do i work around this problem? we've tried everything our 40 some years of combined experience can think of.

Comment
Add comment · Show 2
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 Dave-Carlile · Aug 04, 2015 at 09:49 PM 0
Share

What does the function header for SpawnNewRoom look like? $$anonymous$$ight want to include more of that function ins$$anonymous$$d of just the simplified version.

And what do you mean exactly by "changed to include a reference..."? Is the instantiating game object the only item in the array, or is it added to it? Or something else?

avatar image Vitally · Aug 04, 2015 at 10:07 PM 0
Share

Well, the full function handles a lot of stuff about positioning the prefab and some other stuff that has to do with keeping track of the rooms. I'll include the full piece of code below. at the moment it is a bit of a mess because i've been trying different things to fix it.

what i mean by 'changed to include a reference' is this: the prefab's array is this -roomPrefab -hallwayPrefab -deadendPrefab and the instantiated room (presu$$anonymous$$g it is a hallwayPrefab will look like this) -roomPrefab -(itself) -deadendPrefab and if that room decides to further spawn a hallway it will crate a copy of itself. That breaks the generator and causes all kinds of funky stuff to happen.

http://pastebin.com/T7LD2CLz

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by cjdev · Aug 04, 2015 at 10:04 PM

It looks like you're modifying the parameter (GameObject from your array) in your function and that is what's causing it to change. Try declaring a new variable, assigning it to your parameter, and then modifying that variable instead.

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why is my Prefab Instantiating when the Scene is Loaded? 2 Answers

How to make TCG Deck (Instatiate based on Prefab) 1 Answer

Add prefabs to hierarchy without game running? 1 Answer

Destroying assets is not permitted to avoid data loss. 0 Answers

How do I acces a specific GameObject within the terrain???? 0 Answers


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