- Home /
Canvas starts enabled no matter what
I'm using a canvas to show a player in proximity to something that they can interact e.g. "Press 'E' to activate."
The goal is to have that appear onscreen when in a trigger. The problem is that once the game starts the canvas is already enabled even though I set it too false in code or set it to false in the editor or do both. Once the player has stepped into the trigger and leaves it works properly.
using UnityEngine;
using System.Collections;
public class Trigger_test : MonoBehaviour {
//grab audio
public AudioSource myAudio;
public Canvas pressE;
public Collider myPlayer;
int x = 0;
void Start () {
pressE.enabled = false;
}
void Update () {
}
void OnTriggerStay (Collider myPlayer){
if (myPlayer.attachedRigidbody){
//Debug.Log("Collision works");
if (Input.GetKeyDown("e") && x == 0) {
Debug.Log ("Pause");
myAudio.Pause();
x = 1;
}
else if (Input.GetKeyDown("e") && x == 1) {
Debug.Log ("UnPause");
myAudio.UnPause();
x = 0;
}
}
}
void OnTriggerEnter (Collider myPlayer) {
pressE.enabled = true;
}
void OnTriggerExit (Collider myPlayer) {
pressE.enabled = false;
}
Answer by meat5000 · Oct 11, 2015 at 06:51 PM
Put a debug.log in your OnTriggerEnter to see that there is something triggerring it to begin with. Perhaps the terrain?
Looks like that is the problem. The plane I have everything sitting on is triggering it, but I don't know why. I call the Collider on the firstpersoncontroller so shouldn't only that trigger it?
Check out the collision matrix in the Physics manager.
Also add a condition in to your OnTrigger routine that check which object has triggered it.
void OnTriggerEnter (Collider myPlayer) {
if(myPlayer.gameObject.name == "Whatever")
{
press$$anonymous$$enabled = true;
}
}
You get the idea. There are many ways you can make this work, doesnt have to check for the name.
Your answer
Follow this Question
Related Questions
how to enable or disable a script via code c# 1 Answer
Line Renderer and Canvas Group? 1 Answer
A Canvas inside a Canvas for the purpose of disabling and enabling menus 1 Answer
How to display a message from socket (python) on the screen (Unity) 0 Answers
How to rotate GUI image on cursor drag? 0 Answers