Essentially, I have a gameObject A with a script A attached. I record some
property X of that gameObject A and destroy eventually destroy that object.
A bit later, I have created a new gameObject B and script B.
I'd like to be able to look-up the value of property X I recorded with script B.
Thanks for any ideas/pointers.
Answer by Mike 3
Jun 06, 2010 at 12:42 PM
Use a static variable for anything you want persisted like that - then it belongs to the class as a whole, not the instance
static var score : float = 0;
will persist the score for all versions of the script
Thank you. How do I access it once the game object is destroyed?
Answer by Tetrad
Jun 06, 2010 at 09:50 PM
You could have an intermediary game object that stores the value for you. So you have script C that's on a game object that stays around forever and doesn't do anything but stores/loads values.
You could also use the PlayerPrefs class to store values. These values are actually stored on disk so they persist between sessions unless you specifically delete them.
Or you could go with the static approach.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users post good questions and use the site effectively we have posted a user guide.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
Answers and Comments
No one has followed this question yet.
global variable need advice
Why can't I acces my global variable?
How to use same code at multiple scenes ?
Should I create local copy of global variable?