Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 eshonbel · Jul 23, 2015 at 01:48 PM · guitriggertextunity 4.6

[4.6 JS] How to show GUI text on a trigger enter and then take it away on exit?

I'm doing a kind of level choice scene (like Mario) and I want it to show GUI text (like the name of the level) when the player enters an invisible trigger. Now, this would be easy but since 4.6 it is not as easy as just putting it in a GUI.Label or something (otherwise if you can still do that, please remind me how to then). I'm not really amazing at coding, and everything I have found has been really confusing and hasn't worked (e.g instantiate). Why isn't it so simple anymore? Please just help me show GUI text in a trigger!

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 Runalotski · Jul 24, 2015 at 03:59 PM 0
Share

I have to add this comment as you cannot have more than one answer per thread so

you can change the script so its attached to the player and it will read the name of what it collides with you can do that by adding these changes

 using UnityEngine;
  using System.Collections;
  
  public class SomeScript : $$anonymous$$onoBehaviour {
      
 //This script is now attached to the playerObject
      
 //add this variable to store the level name
      private string levelName = "";
 
      private bool showName = false;
      
 //Add this variable to the method
      void OnTriggerEnter2D(Collider2D col)
      {
         levelName = col.gameObject.name;
          showName = true;
      }
      void OnTriggerExit2D()
      {
          levelName = "";
          showName =false;
      }
      
      void OnGUI()
      {
 //change the display to level name
          if(showName)
              GUI.Label(new Rect(10,10,100,100), levelName);
      }
      
  }

now you just make new objects and you player will find its name, but to get more information you would want to make a level class so you can have information like coins collect, and level times.

but for now this will work

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Runalotski · Jul 23, 2015 at 03:17 PM

To use Triggers you would use unity's functions of

OnTriggerEnter() OnTriggerExit() OnTriggerStay()

to use this make sure one of the objects has a rigidbody2D (assuming its 2D colliders becaus said mario like) and to remove physics tick the IsKinematic (if you dont want them)

and make sure that there are boxColliders2D on each object with IsTrigger Selected

The Least cody looking way of doing it is to add this script to the level object that you wish to show its name, make a new c# script and call it SomeScript

 using UnityEngine;
 using System.Collections;
 
 public class SomeScript : MonoBehaviour {

     //this will tell us if the player
     // has enter or has exit the collider
     private bool showName = false;
     
     //this is a unity function that fires 
     // when this 2dCollider enters another collider
     void OnTriggerEnter2D()
     {
             //we know that the player has just enterd 
             // the collider so we want to show name
         showName = true;
     }

     //this is unity function that fires when
     // this collider exits another collider
     void OnTriggerExit2D()
     {
             //we know the player has left the
             // collider so we dont want to show name
         showName =false;
     }

     //OnGUI is a unity function that will let us use GUI elements
     void OnGUI()
     {   
             //if showName is true then show the lable
         if(showName)
             {
                     //this creats a lable in top left corner and will
                     // display the name of the object from the hierarchy
             GUI.Label(new Rect(10,10,100,100), transform.name);
             }
     }
     
 }

now drop it onto your level object,

now when your char enters it will show the name of your level object at the top and when you leave it will go away.

this is the same code as above but with no comments

 using UnityEngine;
 using System.Collections;
 
 public class SomeScript : MonoBehaviour {
     
     private bool showName = false;
     
     void OnTriggerEnter2D()
     {
         showName = true;
     }
     void OnTriggerExit2D()
     {
         showName =false;
     }
     
     void OnGUI()
     {
         if(showName)
             GUI.Label(new Rect(10,10,100,100), transform.name);
     }
     
 }





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 eshonbel · Jul 23, 2015 at 03:57 PM 0
Share

Thanks, it is basically 2 dimensional but built with 3D, but thanks for the help. I was really confused with how to make the GUI text.

avatar image eshonbel · Jul 23, 2015 at 04:09 PM 0
Share

Do you know if there's a way I can use a script to do this for every level, ins$$anonymous$$d of making a new script for each one?

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

22 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

Related Questions

How change letter spacing 1 Answer

Help me with Gui Text 2 Answers

Display a Text/GUI on screen when triggerd with Fadein/Fadeout 1 Answer

Using a trigger to make a scrollview appear and then disappear? 1 Answer

Make GUI box only appear when I want it to, and disappear again afterwards. 2 Answers


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