r3x
r3x

Reputation: 2147

Calling a button onClick action from another script in Unity

I've been stuck on this for a little while. I want to call an onClick action for a button from another script.

I.E. see the below capture. I want to call that onClick from another script.

enter image description here

I tried this.transform.GetChild(0).SendMessage("onClick", number); but I keep getting SendMessage onClick has no receiver! error.

Any idea what I am missing?

Thank you in advance :)

Upvotes: 0

Views: 4076

Answers (1)

maZZZu
maZZZu

Reputation: 3625

Are you sure that you are sending the message to right GameObject? In your case the Main Camera, not the button. Your code works in my unity 4.6.2. You can print the name of the GameObject with this:

Debug.Log(this.transform.GetChild(0).gameObject.name);

If the GameObject is the right, and the message doesn't still work you can always get the component and call the function old fashion way without messages:

this.transform.GetChild(0).gameObject.GetComponent<ShopBtns>().onClick(number);

Upvotes: 1

Related Questions