- Home /
Duplicate Question
How may i get a variable from another script onto my wanted Script?
Hello Happy Helpers!
I totally would like to know how I could get a variable from one of my scripts and use it in another
I'm using C# Btw!
Well I know how to get a function from another script but I just cant get the knack to getting a variable from another script.
If anyone can help I'd Appreciate it! Yeah@!
I'm sorry about that Static.
I searched up GetComponent solutions in the searches and I didn't find that question.
Guess I was searching wrong then.
$$anonymous$$y bad!
Saying you search is an euphemism. There are hundreds of threads for that matter and tens of detailed ones.
Answer by marijnroukens · Mar 19, 2015 at 08:57 AM
First get the object where the script you are trying to acces is attached to, for example I will use Player as the object:
var Player = GameObject.Find ("Player");
Then get the script that you need, for example the script attached to the player is healthScript. Put this Before you are loading the variable:
healthScript healthScript = Player.GetComponent<healthScript> ();
Then to acces the variable, for example my variable will be playerHealth:
healthScript.playerHealth = 10;
Now the health in healthScript is set to 10!
Good luck with it!
EDIT: I don't know if this is the best way, but as a student this is the only way I am familiar with. Just trying to help!
Thanks for replying I'm still working on it, but now I'm curious.
When you said as a Student. You mean Unity3d Gives Classes to those who want to learn the ways of Unity?
If so is it free classes? Is it online? I'm so curious now.
Update
Everythings working Crystal Clear, thanks for the tip.
Follow this Question
Related Questions
How transform Variable is accessed in Scripts ? 2 Answers
script needs to derive from monobehaviour 1 Answer
Monobehavior class variable become null after assigning value when instantiated 1 Answer
Parent Class variables in Child Class's Inspector (C#) 0 Answers
InvokeRepeating inside of a class 2 Answers