- Home /
How to reference a variable from a different script?
So, after doing some Googling this, I came across many Unity-answers pages about this, however I'm not sure if I'm looking to do something different than what I think I am, or If I'm just not comprehending this. I have a 'Vitals' Script. in the script I have two variables, 'curHealth' and 'maxHealth'. I have another script called 'LevelUp'. This script only holds three variables: 'curEXP' and 'maxEXP' and 'PlayerLevel'. I have an 'if' statement setup to add 1 to the PlayerLevel variable. I'd also like to add 10 to the maxHealth variable in the 'Vitals' script. I'm not sure how to go about specifying the variable from the other script. I'm also using C#, so replies using JavaScript may seem foreign. Thanks for your help, ~~Matt
Hey $$anonymous$$att - please check what your question looks like and edit if it it's wrong. You had indented your paragraph by 4 spaces which is $$anonymous$$D's way of specifying "format as code". I've fixed it for you.
How are your two scripts related to each other? Are they on the same game object? Do the have a reference? http://unitygems.com/gotchas and http://unitygems.com/getcomponent might help
Just to make crystal clear: "How are your two scripts related to each other?" is crucial information to being able to answer this question. Without it the question is not well defined enough to have any distinguishable meaning... In general when posing questions like this you should at the least provide enough information that someone can recreate the problem you are having precisely. Without this it is quite literally impossible to answer meaningfully.
Yes the two scripts are attached to the same gameobject. I apologize for not giving enough information, I'm new here, and at Unity, I was unaware this was crucial to the answer. I see now that you can use "GetComponent" when connected to the same gameobject, but what would I do if they were not connected to the same gameobject? And I wanted to have it set up on a collision event? I understand how to set up the collision event, but how would I reference that in my script? Is there a way to call the script?
Don't worry its a very common problem. Don't blame newness though - this constraint applies to all questions on every subject imaginable. Its just easy to miss out details when you know them all because... well... you know them all and don't need re$$anonymous$$ding like readers of a question do. :)
what would I do if they were not connected to the same gameobject?
You need some way to locate the other game object. if it is instantiated you need to pass that value around - if it has a name you can use GameObject.Find which is documented: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.Find.html
And I wanted to have it set up on a collision event?
What does this mean? Set what up?
Is there a way to call the script?
What do you actually mean by script? Its a very ambiguous term in this context. Stick with 'source file' or 'function' for total clarity please.
Answer by BobbyDoogle · Mar 17, 2014 at 01:59 PM
Matt, as other members mentioned where your scripts are is important. If the scripts are in the same object do so like this:
(IN SCRIPT 1)
public class SCRIPT1NAME : MonoBehaviour{
public SCRIPT2NAME myScriptReference;
void Start()
{ myscriptReference=GetComponent(SCRIPT2NAME); }
(THEN TO REFERENCE THE VARIABLE OF SCRIPT 2 IN SCRIPT 1)
void myMethod()
{ myScriptReference.SCRIPT2VARIABLE; }
} if the Script two is attached to a different game object you will need one of the following:
GameObject.Find("GAME OBJECT NAME WHERE SCRIPT 2 IS").GetComponent();
or
GameObject.FindGameObjectWithTag("TAG APPLIED TO GAME OBJECT WHERE SCRIPT 2 IS ATTACHED").GetComponent<(SCRIPT 2 NAME)>();
Thank you for this response, it really helps to see how it is used in context. This makes alot it easier to visually understand how to do this, Thank you.
No problem, let me know if you get any errors implementing.