x


Accessing Variables (again)

How would I go about accessing variables in another script? I tried this:

var Resources : OtherScript;

But Unity gave me this error:

The name OtherScript does not denote a valid type

Why? It seems perfectly valid to me! Any help appreciated, because I'm totally stuck with a script that should work, that's not working. Thanks in advance...

Elliot Bonneville

EDIT: OtherScript won't show up in the inspector, and I got this script from the docs.

more ▼

asked Mar 22 '10 at 11:11 PM

e.bonneville gravatar image

e.bonneville
5.7k 100 116 165

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

2 answers: sort voted first

"OtherScript" is the name of your other script. You don't literally type OtherScript unless your other script is actually and for real called OtherScript.js. Otherwise you use the script name. It will show up in the inspector once you fix any errors you have.

more ▼

answered Mar 23 '10 at 02:18 AM

Eric5h5 gravatar image

Eric5h5
80.1k 41 132 519

@Eric5h5 Thanks...

Mar 25 '10 at 01:48 AM e.bonneville

Duh... Silly me! I knew that

Mar 25 '10 at 01:49 AM e.bonneville
(comments are locked)
10|3000 characters needed characters left

From the Unity Scripting Reference:

Global variables

You can also create global variables using the static keyword.

This creates a global variable called someGlobal.

// The static variable in a script named 'TheScriptName.js'
static var someGlobal = 5;

// You can access it from inside the script like normal variables:
print(someGlobal); someGlobal = 1;

To access it from another script you need to use the name of the script followed by a dot and the global variable name.

print(TheScriptName.someGlobal);
TheScriptName.someGlobal = 10;
more ▼

answered Mar 23 '10 at 02:12 AM

straydogstrut gravatar image

straydogstrut
1.2k 29 38 60

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

x3323
x819
x262
x108

asked: Mar 22 '10 at 11:11 PM

Seen: 1876 times

Last Updated: Mar 22 '10 at 11:11 PM