- Home /
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.
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?
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.
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.
Your answer
Follow this Question
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