- Home /
How do I make a button appear when my player enters a trigger?
Okay, here's my issue. I have a 3D sidescroller set up with a nice player and a camera that follows him all coded in C#. In this particular sidescroller, I will be giving the opportunity for choices! Yay, right?...Wrong. When my player enters the trigger I have set for the choices, nothing shows up. The buttons are there, but they do not show up in the game view or when test playing. It is VERY frustrating. I have used this particular script before to do a jumpscare, and it has worked, however the buttons simply refuse to show up. Any help would be appreciated. Thank you :) Here is the script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class JumpScare : MonoBehaviour {
public GameObject jumpscareObject;
void Start () {
jumpscareObject.SetActive (false);
}
void OnTriggerEnter (Collider player) {
if (player.tag == "Player") {
jumpscareObject.SetActive (true);
StartCoroutine (DestroyObject ());
}
}
IEnumerator DestroyObject()
{
yield return new WaitForSeconds (5f);
Destroy (jumpscareObject);
Destroy (gameObject);
}
}
P.S. If anyone could also help with another goal I have that I can't seem to figure out, how would I get these buttons to go away upon clicking them?
Answer by DayMoniakk · Sep 02, 2017 at 11:50 AM
Hello, your buttons are separated in another canvas ? If yes, you have to put this canvas order to the foreground by changing the sort order value.
Let me know if its worked.
thank you for replying so quickly. Both of my buttons are in the same canvas, but I figured it out anyway! I needed to change the render mode on the canvas to "world view" so that the main camera could see it. If you're still ready to help, I haven't gotten word on the other question: how to make the buttons disappear after clicking on one of them?
(I'd imagine I'd just make the canvas disappear as one of the button's functions but I'm just not sure how to do that)
I think you can simply do this :
public class HideButton : $$anonymous$$onoBehaviour
{
public GameObject Button;
public void ButtonDisappear ()
{
//Put in this line what your button is doing
Button.SetActive(false);
}
}
You add the button in the inspector window : http://image.noelshack.com/fichiers/2017/35/7/1504434513-1.jpg (for some reasons I can't import screenshots)
And you set the "OnClick ()" of the button in the inspector window : http://image.noelshack.com/fichiers/2017/35/7/1504434518-2.jpg
It's not the best way to do this but it's stay simple
Your answer
Follow this Question
Related Questions
Does OnTriggerStay ignore layers? 1 Answer
Translate Jump Trigger Collision Issue 1 Answer
changing gravity OnTriggerEnter 1 Answer
How to Specify a Collider in OnTriggerStay function? 1 Answer
Simple Triggers problem. 1 Answer