- Home /
Having trouble making text appear when my player hit a obstacle
Hello! I am having trouble making text appear when my player collides with an obstacle. Here is the script I have attached to my “Game Over” text:
using UnityEngine; using UnityEngine.UI;
public class ShowUI : MonoBehaviour { public Text gameOver;
void Start()
{
gameOver.enabled = false;
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Player")
{
gameOver.enabled = true;
}
}
}
While the first part of the script seems to work, the text disappears when I click on play; when I hit an obstacle the text “Game Over” never appears. Here is a short video of the problem: https://drive.google.com/file/d/1CbsXoxP1BB665sijPQyYEthF-7Iw6ajw/view?usp=sharing
Not really sure what I am doing wrong, been looking at other forums and videos to try and fix the problem but nothing seems to be working. Any help would be very appreciated!
The OnCollisionEnter
will never be called because you have attached it to your Text
object. However, your Text
object is not set up to detect collisions, and even if it was correctly set up, your obstacle (3D object) would never collide with your text (UI element).
Either attach this script to your asteroids (your not really make sense) or attach it to your player and change the condition (something like collision.gameObject.tag == "Obstacle"
, but you will certainly need to change this, I don't know your project)
Answer by ADiSiN · Jun 01, 2020 at 02:02 PM
Hi!
So the thing is that you have OnCollisionEnter function in your ShowUI class, so, to make it clear, the OnCollisionEnter in your case will get called when other GameObject with Collider component will contact with the object on which you have the ShowUI class, the function isn't checking all happened Collisions in the game.
I hope that's clear, why it's never get called - because the ShowUI isn't the script that attached to the Asteroids or Player where you checking their Collisions (I assume).
So, as we can see in your video you have Collision detection between your player and your asteroids, so I suggest you to store the reference to the ShowUI in the Player class, so when you call your Death function you will enable the ShowUI gameOver variable.
I hope it helps.
Hello, thank you very much for answering! Yes, I suspected that my OnCollisionEnter function was never being called, because when I tried using a Debug.Log on it, it would never display in the console, but I wasn't entirely sure what to do. I am still pretty new to Unity, so how would I go about storing the reference of ShowUI? Also, I wrote my Death function on the asteroid, not in the player class, so should I put the reference to ShowUI in there?
It will be more complicated and I would say unnecessary to store the ShowUI reference in the asteroid class, because you cannot set up it in the Inspector since you are instantiaing the asteroids in the game time.
Your Player anyway will have script, since you need to controll it, right? So it's better to implement it here and the overall look of this can be something like that:
public ShowUI showUI;
// This one, for example, you call from asteroid class when they hit your player
public void Death()
{
showUI.gameOver.enabled = true;
/* ---- Other code related to Death ---- */
}
So, basically, the Death function in the Player class get called from asteroid class when it hits the player, you can destroy here the player or reduce it's hp and etc. Since we have stored the showUI variable type of ShowUI in the Player class (you can assign it in the inspector simply by drag-drop) we now can access the gameOver variable and change its enabling to true.
Also if you want to have more complicated stuff in ShowUI class when enabling then you can create separate function in the ShowUI, for example:
public void EnablingTheShowUI()
{
gameOver.enabled = true;
/* ---- Other code related to the ShowUI enabling ----- */
}
And call this function from the Player class where you have reference to the ShowUI:
// This one, for example, you call from asteroid class when they hit your player
public void Death()
{
showUI.EnablingTheShowUI();
/* ---- Other code related to Death ---- */
}
It will call the entire function inside the ShowUI class.
The only thing that you should always remember is that the variables or functions must be public in order to be accessed from another classes.
Let me know if that's clear enough and it helped you.
Yes that fixed it, now Game Over displays when my player hits an asteroid! Thank you very much for your help!
Answer by Artik2442 · Jun 01, 2020 at 01:56 PM
Don't use ".enabled" it's easier to create a public GameObject and to use gameObject.SetActive(false/true).
public GameObject GameOverText
void Start()
{
GameOverText.SetActive(false);
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Player")
{
GameOverText.SetActive(true);
}
}
Using .enabled
is totally fine. Using SetActive
won't solve his/her problem.
Ok, do the player have a tag named "Player" or you wanted to use collision.gameObject.name == "Player"
? If the player have the tag, I don't understand what it is happening.
Your answer
Follow this Question
Related Questions
Is it possible to change the size of text in the animator window? 1 Answer
I really want Unity support C++ as scripting API 1 Answer
problem with joints 0 Answers
Checking a bool's state from another script? 1 Answer
How can I call the Load method and/or the ShootingSettings method also only once in the Update ? 1 Answer