- Home /
How to call a function from a script component on another object
Basically I have one object that does nothing but act as a trigger, and another that I want to do something with.
First object has a script component that detects when something gets in the trigger, and does also have a variable set that leads to the target object.
Now, I want to call the function "onLogicInput" on the second object, which was defined in the script component called "LogicCablesSupport" that is added to the target object.
How would I call the function on the second object? C# pleas
Answer by TheCheese · Dec 18, 2012 at 07:31 PM
from your trigger script:
void OnTriggerEnter(){
targetObject.GetComponent<LogicCablesSupport>().onLogicInput();
}
OR - a slightly more efficient way to do it is to make your reference to the target object of type LogicCablesSupport. Then you can simply do:
void OnTriggerEnter(){
targetObject.onLogicInput();
}
NOTE - this is assuming your targetObject variable is public and you've connected the two scripts in the inspector.
For those looking for a JS answer, this would be targetObject.GetComponent(LogicCableSupport).onLogicInput();
(from http://answers.unity3d.com/questions/13248/how-to-call-a-function-from-another-script-getting.html )