I followed a tutorial to make UI text appear over an object when I enter a cube collider and disappear when I exit, is there a way to make the same thing happen with a UI button? The button is supposed to open a window with information on the object and a short turnaround video. I’m using the standard FPSController with an Xbox Gamepad as input. Will i still be able to “click” the button by pressing A if i haven’t got a mouse cursor to hover over the button?
Below is the code from the tutorial to make the UI text appear. I’m fairly new to coding in general, working in C#.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class fadeEffects : MonoBehaviour {
public string myString;
public Text myText;
public float fadeTime;
public bool displayInfo;
void Start ()
{
myText = GameObject.Find("Text").GetComponent<Text>();
myText.color = Color.clear;
}
void Update ()
{
FadeText();
}
void OnTriggerEnter ()
{
displayInfo = true;
}
void OnTriggerExit ()
{
displayInfo = false;
}
void FadeText ()
{
if(displayInfo)
{
myText.text = myString;
myText.color = Color.Lerp(myText.color, Color.white, fadeTime * Time.deltaTime);
}
else
{
myText.color = Color.Lerp(myText.color, Color.clear, fadeTime * Time.deltaTime);
}
}
}
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class fadeEffects : MonoBehaviour {
public Button btn;
void Start(){
btn = Gameobject.Find("Button").GetComponent<Button>();
}
void OnTriggerEnter(Collider other){
if(other.gameObject.tag == "THE TAG OF GAMEOBJECT THE TRIGGER IS SEARCHING FOR"){
btn.gameObject.Setctive(true);
}
}
}
Hi, 1st of all declare a button and give it a name in your script Button MyButton;. Second you have to atach it to a game object as you did for the text : myText = GameObject.Find("Text").GetComponent<Text>();
I precise that if your object is inside a canvas you have to write all the URL for example:
“myText=GameObject.Find(“Canvas/Text”)…”
Now to enable a game object you have to write this code :
MyButton.gameObject.SetActive(true); // this is for enable
// if you want to disable it write this code
MyButton.gameObject.SetActive(false);
For your condition i cannot help you i don’t know how to use colliders but there is alot of tutorial on the web . I hope this will help you and sorry for my bad english @AaronDiscProg
Figured it out, thanks for the replies. In the end I made the button a child of an image and then i was able to fade the image from clear to visible as the character entered a collision trigger. The complicated part was making it clickable when selected and joystick button 0 is pressed. i have a temporary fix for that but I’m working on a better method, the current method disrupts the “first selected” option on the event system.