- Home /
Reference For Button Which Calls OnClick
Hi guys.
I have some buttons which each have an OnClick reference (in the inspector) to my UI handling script. The effect I want to produce is (in part) that when the button is clicked Effect A will happen and the button will disappear. The only thing I could think of to do this was to add a parameter to the function which passes in the GameObject for the button which I would then have to assign in the inspector. This seems somewhat convoluted. Is there an alternative way that I can reference the button which called the function from within the function?
Answer by MrAlter · Mar 11, 2016 at 08:09 AM
I think better solution is pass link to button as parameter:
public void OnClick(Button button) {
button.gameObject.SetActive(false);
}
Also you can receive link to clicked GameObject by EventSystem:
public void OnClick() {
EventSystem.current.currentSelectedGameObject.SetActive(false);
}
Answer by deffitaredina · Mar 10, 2016 at 01:50 PM
you need a script that is similar but different names, and enter the script in evenstystem !!!
Answer by deffitaredina · Mar 10, 2016 at 01:50 PM
pragma strict
public var currentMount : Transform; public var speedFactor : float = 0.1;
function Start () {
}
function Update () {
transform.position = Vector3.Lerp(transform.position,currentMount.position,speedFactor);
transform.rotation = Quaternion.Slerp(transform.rotation,currentMount.rotation,speedFactor);
}
function setMount(newMount : Transform){ currentMount = newMount; }
use this, but yoiu need current mount for moving your camera in one click
Answer by zulfiqar-younus · Mar 11, 2016 at 12:09 AM
make static class and use that class for your effect to play . here is the rough code for this.
publi myClass {
public static PlayEffect() { ///your work } }
void OnClick() { myClass.PlayEffect();/ gameObject.setactive(false); }