- Home /
How do i make a button appear when my FPSController walks through a tigger collider?
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);
}
}
}
Answer by aditya · Mar 05, 2016 at 07:57 AM
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);
}
}
}
Answer by Exceptione · Mar 06, 2016 at 11:26 AM
Hey @AaronDiscProg !
Take a look at the documentation for trigger callbacks: http://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
(If you're using MonoDevelop, you can put the cursor on any method/variable name and press Ctrl+' to bring up the documentation!)
The methods need to take a parameter, to make sure they're being called, you can also write Debug.Log("Here!");
inside the functions to verify.
Answer by danidu93 · Mar 05, 2016 at 01:00 AM
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
Answer by ImNewAtThis · Mar 15, 2016 at 08:50 PM
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.
Your answer
Follow this Question
Related Questions
How to trigger a button click from script 3 Answers
How to instantiate a prefab between Canvas and a Button? 0 Answers
UI button doesn't appear - c# 3 Answers
Button back not clickable after rotation animation 1 Answer
Unity 5 GUI system 1 Answer