- Home /
Access a function from one script in another?
How can I access a function from one script to another one?
I have 2 scripts: trigger.cs player.cs
In the trigger when player enters, I need it to call a Jump() function
if I do it like this
player p = GameObject.Find("Player").GetComponent(player)();
I can access variables but not functions. Using static function is a no go too because not only does it cause many problems, it will also not work if there's more than one player.
So how do I force a player to jump when he enters a trigger?
Also it MUST be executed from trigger.cs file.
.cs is just an example, a js will do too.
Answer by Mortennobel · Apr 19, 2011 at 08:53 PM
Just keep your functions public. Then you can call from one component-class to another.
Answer by DaveA · Apr 19, 2011 at 08:53 PM
http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Components.html
Use public functions
Answer by poncho · Apr 19, 2011 at 08:58 PM
Or you can tell the game object you want, the method you want to run and the parameters you want to give
GameObject.Find("MyGameObject").SendMessage("MyMethod",Parameters);
i use this one, but in this forum i learnt that is not very good in performance =P
Your answer
Follow this Question
Related Questions
"transform" in a static function 1 Answer
Better way to call function from another script from editor script? 1 Answer
How to reference another script and call a function in C# ? 2 Answers
Is there a way to break out of a function and return control to another script's function? 1 Answer
How to Get TextMesh component from the Gameobject the script is attached to? 3 Answers