x


Global Variable accessible from scripts within scene

Hi

I would like to have a global variable that I can access from all scripts in a scene edit / update it.

Any ideas?

Thanks, Chris

more ▼

asked Feb 15, 2010 at 01:13 AM

cmos gravatar image

cmos
557 75 73 87

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

2 answers: sort voted first

You can find such information in the Script Reference under the section "Global Variables" (Link to Script Reference)

more ▼

answered Feb 15, 2010 at 01:45 AM

Sebas gravatar image

Sebas
4.9k 26 34 71

Thanks for your response. Is there a way to reference a Label value from another script? I created a GUI label with something like this: GUI.Label (Rect (100, 25, 100, 30), text) .... how can I retrieve the value stored in it from another script?

Feb 15, 2010 at 02:17 AM cmos

answered/edited this in your other question. see this answer http://answers.unity3d.com/questions/4009/how-to-make-a-timer-in-the-game

Feb 15, 2010 at 02:34 AM Sebas

quick answer here: the label value is nothing but a normal string variable. You simply declare the variable at the beginning of your script as member variable (outside of any functions) and access it through other scripts like described in your other question which I linked in the previous comment.

Feb 15, 2010 at 02:36 AM Sebas
(comments are locked)
10|3000 characters needed characters left

you have two ways to do this. first of all you can access public variables of scripts from other scripts using GetComponent like what sebas said. also you can create a static class and create static variables in it and use them anywhere in your game. simply create a static class and use it's name to get a reference to it.

static class SceneData
{
static int width=100;
static GameObject soundManager;
}
//usage
void Start ()
{
SceneData.soundManager=GameObject.find ("sound");
}
void Update()
{
SceneData.width=time.time;
}

you can use sceneData class in any script in any gameObject.

more ▼

answered Feb 15, 2010 at 05:41 AM

Ashkan_gc gravatar image

Ashkan_gc
10.1k 77 105 165

(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:

x1402

asked: Feb 15, 2010 at 01:13 AM

Seen: 16776 times

Last Updated: Feb 15, 2010 at 01:13 AM