- Home /
How do you access a script attached to another object
The problem is i need to access an animation script from another script how do i do that???
Please help i dont think there is any need to show an example as it is a simple question well i think it is!
Answer by Cameron 3 · May 11, 2011 at 05:37 PM
I use C#, but usually what I do is:
1) Add a public variable to the script that wants to access the other script. So for example, if I have a script called "Death" and I want to access "MyAnimationScript", which is attached to some game object that is already instantiated in the editor, i can do:
public class Death : MonoBehavior {
public MyAnimationScript someScript;
void Start () {
}
void Update () {
}
}
2) If my death script is attached to some game object in the editor, that game object will have a slot called "someScript" and I can drag the game object with my animation script on top of it.
Alternatively, you can search for the other script using GameObject.FindGameObjectWithTag and GetComponent. So, if my Animation script was on a game object that had a tag called "PlayerAnimation", I could do:
public class Death : MonoBehavior {
private MyAnimationScript someScript;
void Start ()
{
someScript = GameObject.FindGameObjectWithTag("PlayerAnimation").GetComponent<MyAnimationScript>();
}
void Update () {
}
}
Now I can access that script and all of it's variables in my Death script by using
someScript.whatever
Answer by CHPedersen · May 11, 2011 at 12:23 PM
ScriptThatYouWant = GameObject.Find("[GameObjectName to which target script is attached]").GetComponent<[Name of target script]>();
This doesn't seem to work could you more detail am i meant to keep the [] in the script the script i am writing the code in is Die Script and the script i want to access to play an animation is Walk
No, remove the [] (brackets). They simply indicate that you are meant to replace the text with your own classnames and object names.
This should be the best answer. Since Object names are more unique, tags being useful for dynamic object addition and removal.
Works very well in my case:
waveSpanner = GameObject.Find("Wave$$anonymous$$anager").GetComponentInChildren<WaveSpanner>();
print(" waveCount-->" + waveSpanner.waveCount);
Answer by FLASHDENMARK · May 11, 2011 at 01:55 PM
I am not completely sure what you are trying to achive, but:
//When using static variables you can access them from other scripts //Call this script TEST. static var yourVariable = 10;
//This is a new script function Update () { (script name) TEST.yourVariable(the variable) += 10 * Time.deltaTime; }
Answer by john 9 · May 14, 2011 at 12:59 PM
Sorted it thanks guys just added an ontriggerenter to the animation script
Answer by parmardarshanv · Jan 16, 2020 at 01:11 PM
My personal best way is: Write the following line of code(outside start or update or any methods)
public static currentscriptname instant
and in the start function of the same script, write:
instant = this;
If you want to access the script in any other script, write:
/*scriptyouwanttoaccess*/.instant./*whatever you want to do*/
Your answer
