NGUI - Simulate a button press from script

Hey all,
I purchased NGUI a couple weeks ago, which is a pretty terrific package (although I was slightly bummed when two days after I bought it it went on sale for half the price, but what are you gonna do).

NGUI has useful scripts for tweening, triggering a sound, etc when you click on a button, so I was wondering how to script to have a button “click itself” by triggering it from another script?

The button scripts in NGUI receive notice of a click through the OnClick() method. To trigger the click from another script call send this message to all the components on the button. For instance, if you have a script on the button that should “click” the button when the ‘T’ key is pressed then the code to do this would be:

void Update() {
    if (Input.GetKeyDown(KeyCode.T)) {
        SendMessage("OnClick");
    }
}

If the script is on a different game object to the button, then you will need to call SendMessage() on the button’s game object.

Other NGUI button events can also be triggered this way. For instance: SendMessage("OnHover", true) would trigger the button to act like the mouse is hovering over it (remember to send the corresponding false message when you want the hover effect to stop).

why do you actually need that? You could solve it by:

FirstButtonScript.cs:

using UnityEngine;
using System.Collections;

public class FirstButtonScript : MonoBehaviour {
	public GameObject buttonToTrigger; // drag your button you want to trigger here in inspector

	void OnClick () {
		buttonToTrigger.GetComponent<ButtonToTriggerScript>().ClickMe();
	}

}

ButtonToTriggerScript.cs:

using UnityEngine;
using System.Collections;

public class ButtonToTriggerScript : MonoBehaviour {

	void OnClick () {
		ClickMe();
	}
	
	void ClickMe() {
		// Do whatever you want
	}
}

Thanks for your reply Bjørn, but based on your response I feel I must have been too vague in my question. When I said:

“NGUI has useful scripts for tweening, triggering a sound, etc when you click on a button,” and asked for how to trigger a button being pressed from script,

the reason this would have been useful is that I had numerous scripts attached to each button, and each script was specifying a dozen variables configured in the editor (with which I was tweening objects, playing sounds, etc).

Simply triggering the script like you suggested negated the usefulness of much of the editor customizations on those scripts.

My solution: I removed all of my NGUI iTween scripts, since they can’t easily be accessed through scripting, and instead replaced their functionality by buying Playmaker and using the built in iTween commands in that package. So far so good.

Thanks anyway!

Oh and in case it helps somebody, here is a paired down version of some of my script.

Script 1:

import System.Collections.Generic;

var StateMachine : PlayMakerFSM;

function Start() {
    state = "select";
}

function back(){
    if(state == "select"){
		//button not available
    }else if(state == "customize"){
		modeCharSelect();
    }else if (state == "whack"){
    	var funct = (currentChar==char01)?"modeChar01Customize":(currentChar==char02)?"modeChar02Customize":"nil";
    if(funct!="nil")
    	gameObject.SendMessage(funct);
    }else if (state == "stuff"){
		modeWhack();
    }else if (state == "send"){
		modeStuff();
    }
}


function modeChar01Customize () {
	state = "customize";
	StateMachine.Fsm.Event("modeChar01Customize");
	Debug.Log(state);
}

Script 2:

 var mainCamera: GameObject;

//can't use onmouseup code because I'm developing for iOS:
    function OnClick(){
        mainCamera.GetComponent(_MAIN).back(); 
    }