Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by pc1120 · Jun 01, 2020 at 01:45 PM · script.

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hellium · Jun 01, 2020 at 02:02 PM 1
Share

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)

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pc1120 · Jun 01, 2020 at 02:18 PM 0
Share

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?

avatar image ADiSiN pc1120 · Jun 01, 2020 at 02:35 PM 1
Share

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.

avatar image pc1120 ADiSiN · Jun 01, 2020 at 03:04 PM 0
Share

Yes that fixed it, now Game Over displays when my player hits an asteroid! Thank you very much for your help!

Show more comments
avatar image
-2

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); 
      }
  }



Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hellium · Jun 01, 2020 at 01:59 PM 0
Share

Using .enabled is totally fine. Using SetActive won't solve his/her problem.

avatar image Artik2442 Hellium · Jun 01, 2020 at 02:04 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

159 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges