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 *> UnityEngine.Random.Range (0, 100))*
  •  {*
    

_ SpawnNewRoom(prefabSections*);_
_
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.

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.