x


How do you return variables from other scripts?

Ok, I know the first admin to see this will think "This has been asked before! ~delete~" but I just don't understand! The other instances where this has been asked seems to be quite specific to their needs. Rather than saying "This is my code, make it do this" can someone please explain to me, using a random piece of code, how to call a variable from another script? If you do need to see my script I'll put it on but I'd prefer to learn this rather than copy it :) Thanks.

more ▼

asked Jul 15 '12 at 01:45 PM

venhip gravatar image

venhip
167 7 13 16

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

2 answers: sort voted first

JS:

  anotherGameObject.GetComponent(ScriptName).myVariable = 1;

  GameObject.Find("someName").GetComponent(ScriptName).DoSomething();

C#:

 anotherGameObject.GetComponent<ScriptName>().myVariable = 1;

 //Find the first instance of the script on the object or any children
 GameObject.Find("someName").GetComponentInChildren<ScriptName>().DoSomething();
more ▼

answered Jul 15 '12 at 02:25 PM

whydoidoit gravatar image

whydoidoit
33.1k 12 23 101

...and look at the Unity manual from http://docs.unity3d.com/Documentation/ScriptReference/index.html. The part above is under "Accessing Other Components."

There are a lot of other good sections that are buried 6 links down from somewhere -- you just have to use a combination of the links in Unity combined with Google and be prepared to bookmark.

Jul 15 '12 at 03:35 PM Owen Reynolds

Ok, I think I get it but I'm returning an error on this:

if(gameObject.GetComponent<HealthBar>().stamina >= 0.0)

Any ideas (i have a script named "HealthBar" with a variable "stamina" in it and its attached to the same object as this script) - Thanks for your answers as well!

Edit: error says: "The type or namespace name `HealthBar' could not be found. Are you missing a using directive or an assembly reference?"

Jul 15 '12 at 09:33 PM venhip

No, its C#, and I'm sure it is. The variable in the other script is java but ^ this one is in C# because its a modified script from somewhere else. Thanks.

Jul 15 '12 at 09:48 PM venhip

well if that's in JS it should be

gameObject.GetComponent(HealthBar).stamina

Just make sure you stamina variable is accessible.

Jul 15 '12 at 09:44 PM trickpirata

In C#:

gameObject.GetComponent<HealthBar>().stamina 

Just make sure that the healthbar is attached with the current gameObject that your workin on.

Jul 15 '12 at 09:51 PM trickpirata

Sorry to be the bearer of bad news. It's a total killer - you really should stick with one language - makes life a lot easier.

Jul 16 '12 at 11:36 PM whydoidoit
(comments are locked)
10|3000 characters needed characters left

I'm fairly certain if you declare the variable as "Public Static (Type) (VariableName)", it should be accessible outside of the script by using "ScriptName.VariableName".

But note that declaring it as static means there will only be 1 of that variable, so you don't want to use it for something that multiple objects have, like an enemy's health.

more ▼

answered Jul 15 '12 at 02:23 PM

NothAU gravatar image

NothAU
30

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

x344
x344
x45
x21
x13

asked: Jul 15 '12 at 01:45 PM

Seen: 614 times

Last Updated: Jul 16 '12 at 11:36 PM