Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Text Box Startup

Discussion in '2D' started by LucaBGT, Feb 22, 2015.

  1. LucaBGT

    LucaBGT

    Joined:
    Feb 20, 2015
    Posts:
    22
    I have problem with my 2d Game.
    I want the player to touch a certain object, and then a text popping up.
    This is the place: Unity Forum 3.png
    Now, I'm a beginner, but I think I've got an idea. I dropped a Text into the scene. Unity Forum 4.png
    This looked pretty good.
    I thought it would be a good idea to put a variable in this text. Unity Forum 4.png Now, the plan says that when the player touches the place, a script is activated that changes the string to what should go there, for Example:
    -String is empty
    -Player touches Area -> String changes to: "I should get going now."
    -String changes back.

    I have a little problem that causes all the other ones: I'm a Unity-Noob.
    How do I mark the area? I don't know.
    How do I put a String in the Text? I don't know.
    How do I write a script that changes the String? I don't know.
    Does this plan even make any sense? I DON'T KNOW.

    Help, please!
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    Mark the area by creating an empty game object. Attach a collider to this, for example a box collider. Move the object where you want the area to be and make the box collider what ever size you need.
    Attach a script with something like this to the empty object:
    Code (CSharp):
    1. using System.Diagnostics;
    2. using System.Net.Mime;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using System.Collections;
    6.  
    7. public class ShowText : MonoBehaviour
    8. {
    9.     private Text message;
    10.  
    11.     void Start()
    12.     {
    13.         message = GameObject.Find("Message").GetComponent<Text>();
    14.  
    15.         message.text = "";
    16.     }
    17.  
    18.     void OnTriggerEnter2D(Collider2D other)
    19.     {
    20.         if (other.gameObject.tag == "Player")
    21.         {
    22.             message.text = "Hello there!";
    23.         }
    24.     }
    25.  
    26.     void OnTriggerExit2D(Collider2D other)
    27.     {
    28.         if (other.gameObject.tag == "Player")
    29.         {
    30.             message.text = "";
    31.         }
    32.     }
    33. }
    34.  
    The script assumes that you have placed an UI Text somewhere in the scene and that it is named "Message". It also assumes that your player is tagged with "Player".

    I hope this will get you started!
     
  3. LucaBGT

    LucaBGT

    Joined:
    Feb 20, 2015
    Posts:
    22
    Thanks for your help!
    Honestly, I couldn't test the script because it gave me the "The referenced script on this Behavior is missing" error. I'trying to fix this one first and then try your code.
     
  4. LucaBGT

    LucaBGT

    Joined:
    Feb 20, 2015
    Posts:
    22
    Sadly, the script did not work properly. It turns the text to "", which means the first part (the void start() ) worked, but nothing changed when I hit the GameObject. It has got a BoxCollider and the Player is tagged Player, the Text is called message too. I don't see the problem.
    What can I do now?
     
  5. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    Weird, it works for me. I've included the little test project I did. So you can test if that works for you...

    Damn, for some reason I can't upload the file. Get it from http://gnulix.org/TextTest.zip instead.
     
    Last edited: Feb 23, 2015
  6. LucaBGT

    LucaBGT

    Joined:
    Feb 20, 2015
    Posts:
    22
    Okay, now it worked!
     
  7. LucaBGT

    LucaBGT

    Joined:
    Feb 20, 2015
    Posts:
    22
    I have another question, so...
    I want that the character stops movement for a while (input disabled), so that i can run through a short cutscene.
    I have a script that looks something like this:
    Code (CSharp):
    1. using System.Diagnostics;
    2. using System.Net.Mime;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using System.Collections;
    6.  
    7. public class ShowMessage : MonoBehaviour
    8. {
    9.     private Text message;
    10.    
    11.     void Start()
    12.     {
    13.         message = GameObject.Find("Message").GetComponent<Text>();
    14.        
    15.         message.text = "";
    16.     }
    17.    
    18.     void OnTriggerEnter2D(Collider2D other)
    19.     {
    20.         if (other.gameObject.tag == "Player")
    21.         {
    22.             yield return new WaitForSeconds(5f);
    23.             //At this point, the input should be disabled.
    24.             message.text = "I better get going now...";
    25.             message.text = "";
    26.             yield return new WaitForSeconds(5f);
    27.             //Here, the input should be enabledd again
    28.             Destroy (this);
    29.         }
    30.     }
    31.    
    It would be very nice if you could help me with this too...
    Thanks in advance!
     
  8. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    If you change your ShowMessage code to this:

    Code (CSharp):
    1. public class ShowMessage : MonoBehaviour
    2. {
    3.     private Text message;
    4.  
    5.     public static bool onHold = false;
    6.  
    7.     void Start()
    8.     {
    9.         message = GameObject.Find("Message").GetComponent<Text>();
    10.  
    11.         message.text = "";
    12.     }
    13.  
    14.     void OnTriggerEnter2D(Collider2D other)
    15.     {
    16.         if (other.gameObject.tag == "Player")
    17.         {
    18.             message.text = "I better get going now...";
    19.  
    20.             StartCoroutine(ShortPause());
    21.         }
    22.     }
    23.  
    24.     IEnumerator ShortPause()
    25.     {
    26.         onHold = true;
    27.         yield return new WaitForSeconds(1f);
    28.         onHold = false;
    29.         message.text = "";
    30.         Destroy(this);
    31.     }
    32. }
    33.  
    Then in your movement script you can check ShowMessage.onHoldl. If it is true, then you shouldn't check input, otherwise you should check it.