I have a button on my stage ( Camera - > Canvas → JumpButton).
I have prefab that is instantiated at runtime. That prefab has a VehicleController script (C#) that contains a Jump() function.
All I want to do is simply add listener (in my VehicleController script) to that button so that it fires the Jump() function.
I’ve seen this post: Unity 4.6: GetComponent().onClick... how do you add an event to button click? - Unity Answers but I cannot get it to work with my setup. I’m having trouble targeting or access by name the JumpButton that is already in the scene.
In my VehicleController script:
Button JumpButton = null; // assign in the editor?
void Start() {
//JumpButton = //How do I find the desired button in the scene?
JumpButton.onClick.AddListener(() => {MyJumpFunction(); });
}
Any help would be greatly appreciated.