x


Preserve global variable after game object destroyed?

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.

more ▼

asked Jun 06, 2010 at 12:39 PM

pickledzebra gravatar image

pickledzebra
316 92 71 81

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Use a static variable for anything you want persisted like that - then it belongs to the class as a whole, not the instance

e.g.

static var score : float = 0;

will persist the score for all versions of the script

more ▼

answered Jun 06, 2010 at 12:42 PM

Mike 3 gravatar image

Mike 3
33k 21 86 294

Thank you. How do I access it once the game object is destroyed?

Jun 07, 2010 at 11:25 AM pickledzebra
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jun 06, 2010 at 09:50 PM

Tetrad gravatar image

Tetrad
8.1k 62 65 131

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1383
x166
x4

asked: Jun 06, 2010 at 12:39 PM

Seen: 1991 times

Last Updated: Jun 06, 2010 at 12:39 PM