- Home /
I have a problem with making game over canvas visibility on and off.
Hello, I am making game over menu. I have canvas and i put some text in it and when I play the canvas dissapear but when I hit collider nothing happens, I was trying to Debug text on collider but dosen't work. I am stuck.
Code:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class gameover : MonoBehaviour { public Canvas cav; public bool x; public Collider coli;
private void Start()
{
cav = GetComponent<Canvas>();
x = false;
cav.gameObject.SetActive(false);
coli = GetComponent<Collider>();
}
void OnCollisionEnter(Collision coli)
{
if (coli.gameObject.tag == "toto")
{
Debug.Log("lolz");
x = true;
}
}
private void Update()
{
if (x == true)
{
cav.gameObject.SetActive(true);
}
if (x == false)
{
cav.gameObject.SetActive(false);
}
}
}
Comment
your player setup correctly? and it’s not a 2d collison?
Answer by KittenSnipes · Sep 07, 2018 at 09:46 PM
@wister808 For some reason you were getting a needless Collider. Maybe it was interfering with your collision function. Hope this works now
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class gameover : MonoBehaviour {
public Canvas cav;
public bool x;
private void Start()
{
cav = GetComponent<Canvas>();
x = false;
cav.gameObject.SetActive(false);
}
void OnCollisionEnter(Collision coli)
{
if (coli.gameObject.tag == "toto")
{
Debug.Log("lolz");
x = true;
}
}
private void Update()
{
if (x == true)
{
cav.gameObject.SetActive(true);
}
if (x == false)
{
cav.gameObject.SetActive(false);
}
}
}
Your answer