- Home /
C# override a destroyed GameObject in a List
Hello,
I try to restore already destroyed GameObjects. Therefore I created a struct which is saving the whole GameObject, the number which is written in a TextMesh "on" my GameObject and the Prefab which I used to spawn it. Then I created a List of this struct.
private struct NumObjectStruct{
private GameObject GO;
private int num;
private Transform pref;
public NumObjectStruct(GameObject GO, int num, Transform pref){
this.GO = GO;
this.num = num;
this.pref = pref;
}
public GameObject getGO(){
return this.GO;
}
public int getNum(){
return this.num;
}
public Transform getPref(){
return this.pref;
}
public void setGO(GameObject vGO){
GO = vGO;
}
public void setNum(int vNum){
num = vNum;
}
public void setPref(Transform vPref){
pref = vPref;
}
}
List<NumObjectStruct> SpawnedNumberObjects = new List<NumObjectStruct>();
When I generate a new number I am spawning a new GameObject.
GameObject Inst;
Inst = (Instantiate (shownObject, spawnPos, transform.rotation) as Transform).gameObject;
NumObjectStruct newStructForList = new NumObjectStruct(Inst, rndNumber, shownObject);
SpawnedNumberObjects.Add (newStructForList);
//End Spawning
Transform TextMeshTransform = SpawnedNumberObjects[currentNumberInd].getGO ().transform.FindChild ("NumberText");
TextMesh NumberText = TextMeshTransform.GetComponent(typeof(TextMesh)) as TextMesh;
NumberText.text = rndNumber.ToString ();
shownObject is a public Transform which I set with one of my Prefabs, spawnPos is a Vector3, rndNumber is an integer of the number I generated and currentNumberInd is an integer which I am using as counter for how many Objects are in my List. The default value of the counter is 0 and after spawning a new Object I am increasing it by one.
During my Program there will be the situation that I have to Destroy one of those objects.
void DestroyNumberObject(){
Destroy(SpawnedNumberObjects[DestroyIndex].getGO ());
}
Now my problem comes: There is the situation that I need to restore one of the GameObjects I already destroyed. Therefore I also saved my number and the prefab inside my struct. Then I instantiate it again and try to override the destroyed GameObject in the List with my new recreated GameObject.
GameObject ReSave;
ReSave = (Instantiate (SpawnedNumberObjects[currentNumberInd-1-AnimNodeAmount].getPref().transform, spawnStornoPos, transform.rotation) as Transform).gameObject;
print ("resave: " + ReSave);
Transform TextMeshTransform = ReSave.transform.FindChild ("NumberText");
TextMesh NumberText = TextMeshTransform.GetComponent(typeof(TextMesh)) as TextMesh;
NumberText.text = SpawnedNumberObjects[currentNumberInd-1-AnimNodeAmount].getNum ().ToString ();
SpawnedNumberObjects[currentNumberInd-1-AnimNodeAmount].setGO(ReSave);
print ("resave in list: "+SpawnedNumberObjects[currentNumberInd-1-AnimNodeAmount].getGO ());
The "AnimNodeAmount" is also an integer. This value is "4" because there are 4 Objects shown on my display. The rest in the array is either destroyed or doesn´t exist (anymore).
My Output is:
resave: NumberObjectRed(Clone) (UnityEngine.GameObject)
resave in list: null
Why is the value inside my struct null now? What am I doing wrong?
Any help is appreciated. Thank you in advance.
Answer by Bunny83 · Oct 24, 2012 at 08:10 PM
Your problem is that you used the combination of a value type (your struct) and a List. When ever you access an element in your list you will get a copy of your struct.
This line:
SpawnedNumberObjects[currentNumberInd-1-AnimNodeAmount].setGO(ReSave);
will execute the get-method of the index-property of your List object. The getter will return a copy of your requested element. On this copy you invoke setGO.
You either have to use a class instead of your struct, so you have a reference type and you always work on the same instance, or do it like this:
NumObjectStruct tmp = SpawnedNumberObjects[currentNumberInd-1-AnimNodeAmount]; // invoke getter
tmp.setGO(ReSave); // change the temp object
SpawnedNumberObjects[currentNumberInd-1-AnimNodeAmount] = tmp; // invoke setter