KujaEx
1
Hello,
I created a GUI Text and a GUI Button. Now, by clicking on the button, the GUI Text has to be activated or deactivated when it’s already displayed. I tried to attach a script to the button, but it doesn’t really work…
using UnityEngine;
using System.Collections;
public class Help : MonoBehaviour {
public GameObject helpText;
void Start() {
helpText.SetActive(false);
}
public void onClick() {
if(helpText.activeSelf) {
helpText.SetActive(false);
}
else {
helpText.SetActive(true);
}
}
}
And can’t find answers to similar problems here… most of them are outdated for Unity 3 or 4… don’t know, how the “On Click()” in the button settings shall work.
Is there an easy way to do this?
Thanks for help.
Rytje
2
Your script doesn’t know what helpText is. You probably need to use GameObject.Find() or something. You don’t have to call the method you want to call with the button OnClick. You can call it anything as long as it is public I think. In your button you need to go to the OnClick method, select your script and select your public method.
KujaEx
3
Okay, I watched now this tutorial: https://unity3d.com/learn/tutorials/modules/beginner/ui/ui-button
And made it that way with that script:
using UnityEngine;
using System.Collections;
public class Help : MonoBehaviour {
public GameObject startHelpText;
void Start() {
startHelpText.SetActive(false);
}
public void switchHelp(GameObject helpText) {
if(helpText.activeSelf) {
helpText.SetActive(false);
}
else {
helpText.SetActive(true);
}
}
}
I put that Script on an empty object and put that object on the “On Click ()” thing in the button and choose the "switchHelp(GameObject helpText) " in there. It’s working now… but I thought it could be easier…