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 Nanashi91 · Oct 24, 2012 at 07:49 PM · c#instantiatelistdestroystruct

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.

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
Best Answer

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

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 Nanashi91 · Oct 25, 2012 at 08:35 AM 0
Share

Thank you a lot. It worked. :)

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

10 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

Related Questions

Store Game Object Into List For Later Reinstantiation? 0 Answers

Is there anyway to delete clones that are local variables? 1 Answer

References to GameObject become null 1 Answer

A node in a childnode? 1 Answer

How to make TCG Deck (Instatiate based on Prefab) 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