Search Unity

Car text trigger

Discussion in 'Immediate Mode GUI (IMGUI)' started by Melchet, Feb 5, 2015.

  1. Melchet

    Melchet

    Joined:
    Feb 5, 2015
    Posts:
    15
    Hello all,

    I'm making an horror game.

    I'd like an script on a car that if you press "E" an text appear.

    And after press "E "again the text dissapears..

    Some1 can help me?

    Regards,

    Melchet
     
  2. boolfone

    boolfone

    Joined:
    Oct 2, 2014
    Posts:
    289
    Maybe try a script with something like this:

    Code (CSharp):
    1.         bool textShowing = false;
    2.  
    3.         void Update ()
    4.         {
    5.                 if (Input.GetKeyDown (KeyCode.E))
    6.                         textShowing = !textShowing;
    7.         }
    8.  
    9.         void OnGUI ()
    10.         {
    11.                 if (!textShowing)
    12.                         return;
    13.  
    14.                 GUIStyle style = new GUIStyle ();
    15.                 style.fontSize = 130;
    16.                 style.normal.textColor = Color.white;
    17.                 GUI.Label (new Rect (10, 10, 100, 20), "Hello World!", style);
    18.         }
     
  3. Melchet

    Melchet

    Joined:
    Feb 5, 2015
    Posts:
    15
    I get 4 errors:

    Error line 14 and 17
     
  4. boolfone

    boolfone

    Joined:
    Oct 2, 2014
    Posts:
    289
    What do the errors say?
     
  5. Melchet

    Melchet

    Joined:
    Feb 5, 2015
    Posts:
    15
    Here I taken an screenshot from it car text script.jpg
     
  6. Melchet

    Melchet

    Joined:
    Feb 5, 2015
    Posts:
    15
    I want put the script on this car car unityy 3d.jpg
     
  7. Melchet

    Melchet

    Joined:
    Feb 5, 2015
    Posts:
    15
    the errors errors.jpg errors.jpg
     
  8. boolfone

    boolfone

    Joined:
    Oct 2, 2014
    Posts:
    289
    I think there are two issues.

    One issue is that I think it is a problem to put a space in the name of a Unity script. So, you should change “car text” to “cartext” or something without a space.

    Also, here is some code that assumes you have named your script “cartext”:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class cartext : MonoBehaviour
    5. {
    6.         bool textShowing = false;
    7.  
    8.         void Update ()
    9.         {
    10.                 if (Input.GetKeyDown (KeyCode.E))
    11.                         textShowing = !textShowing;
    12.         }
    13.    
    14.         void OnGUI ()
    15.         {
    16.                 if (!textShowing)
    17.                         return;
    18.        
    19.                 GUIStyle style = new GUIStyle ();
    20.                 style.fontSize = 130;
    21.                 style.normal.textColor = Color.white;
    22.                 GUI.Label (new Rect (10, 10, 100, 20), "Hello World!", style);
    23.         }
    24. }
    The code I gave you before was a snippet and wasn’t intended to occupy the whole file.
     
  9. Melchet

    Melchet

    Joined:
    Feb 5, 2015
    Posts:
    15
    Hello boolfone,

    the script worked.

    But now I got the problem if I press the 'E ' key the text shows anywhere..

    I have putted the script on the car itself
     
  10. boolfone

    boolfone

    Joined:
    Oct 2, 2014
    Posts:
    289
    You can move the text by setting the first two parameters to the Rect constructor in this line:

    GUI.Label (new Rect (10, 10, 100, 20), "Hello World!", style);

    Also, if you want, you can use this script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class cartext : MonoBehaviour
    5. {
    6.         bool textShowing = false;
    7.         public float x;
    8.         public float y;
    9.  
    10.         void Update ()
    11.         {
    12.                 if (Input.GetKeyDown (KeyCode.E))
    13.                         textShowing = !textShowing;
    14.         }
    15.  
    16.         void OnGUI ()
    17.         {
    18.                 if (!textShowing)
    19.                         return;
    20.      
    21.                 GUIStyle style = new GUIStyle ();
    22.                 style.fontSize = 130;
    23.                 style.normal.textColor = Color.white;
    24.                 GUI.Label (new Rect (x, y, 100, 20), "Hello World!", style);
    25.         }
    26. }
    Then you can adjust the x and y position for the text like so:

     
  11. Melchet

    Melchet

    Joined:
    Feb 5, 2015
    Posts:
    15
    Thx for the video.

    but that isn't actualy the answer on my question:).

    the problem is the text what belong with the car gonna show up everywhere when I press the 'E"button.

    It only needs to be showing at the car.
     
  12. boolfone

    boolfone

    Joined:
    Oct 2, 2014
    Posts:
    289
    You can decrease the font size by modifying this line:

    Code (CSharp):
    1. style.fontSize=130;
     
  13. Melchet

    Melchet

    Joined:
    Feb 5, 2015
    Posts:
    15
    Let me explain better,

    This picture is the good position when I pressing 'E'.

    Now the girl stances to the car Good position.jpg

    This picture is the bad one, here I press 'E' too but without the car. I need the text only shown up at the car as the picture above.

    bad position.jpg
     
  14. Melchet

    Melchet

    Joined:
    Feb 5, 2015
    Posts:
    15
    How do I make that possible?