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

Changing 4.6 UI text with OnTriggerEnter

Discussion in 'Scripting' started by ATLAS-INTERACTIVE, Jan 17, 2015.

  1. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I am looking to add a sort of hints box to the bottom of the screen, mostly for triggering, but I don't think it is efficient to create 50+ of the things, is there any way to change the 4.6 Text UI code in script?
    I work primarily in Javascript and would prefer to be able to understand it, so i'm not sure how useful C# will be to me, there are other things I will be adding to this.

    Thanks.
     
  2. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    Not entirely sure on JS implementation, but in C# it's as simple as
    Text textbox = someTextComponent;
    textbox.text = "Blah";
     
  3. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Using this I get the error listed below:

    Code (JavaScript):
    1. import UnityEngine.UI;
    2.  
    3. var text = GameObject;
    4.  
    5. var textToUse = Text;
    6.  
    7. private var drawGUI = false;
    8.  
    9. function Start () {
    10.  
    11. }
    12.  
    13. function Update () {
    14.  
    15. }
    16.  
    17. function OnTriggerEnter (theCollider : Collider)
    18. {
    19.     if (theCollider.tag == "Player")
    20.     {
    21.         drawGUI = true;
    22.     }
    23. }
    24.  
    25. function OnTriggerExit (theCollider : Collider)
    26. {
    27.     if (theCollider.tag == "Player")
    28.     {
    29.         drawGUI = false;
    30.     }
    31. }
    32.  
    33. function onGUI ()
    34. {
    35.     if (drawGUI == true)
    36.     {
    37.         text = GetComponent(Text);
    38.         text.text = textToUse;
    39.     }
    40. }
    Assets/Scripts/Events/Hints.js(37,28): BCE0022: Cannot convert 'UnityEngine.UI.Text' to 'System.Type'.
     
  4. shadow-river

    shadow-river

    Joined:
    May 29, 2013
    Posts:
    63
    don't use OnGui try this.

    Code (JavaScript):
    1. #pragma strict
    2. import UnityEngine.UI;
    3.  
    4. var textToUse = "Text";
    5. var text : Text;
    6. private var drawGUI = false;
    7.  
    8. function Start ()
    9. {
    10.     text.text = "";
    11. }
    12. function Update ()
    13. {
    14.     if (drawGUI == true)
    15.         {
    16.             text.text = "" + textToUse;
    17.         }
    18.        
    19.      
    20.  
    21. }
    22.  
    23. function OnTriggerEnter (theCollider : Collider)
    24. {
    25.     if (theCollider.tag == "Player")
    26.     {
    27.         drawGUI = true;
    28.     }
    29. }
    30. function OnTriggerExit (theCollider : Collider)
    31. {
    32.     if (theCollider.tag == "Player")
    33.         {
    34.             drawGUI = false;
    35.             text.text = "";
    36.         }
    37. }
    38.  
    39.  
    when you use variables you need to feed it into a string which is what "" is for. and you don't need to use ongui any more. for what your wanting its best not to use "text = GetComponent(Text);" because if you use that you need to drop the script onto the ui text element so that it can get the text component if you do that the Ontrigger stuf wont work . when you dont youse "text = GetComponent(Text);" you can put the script anywhere and drop the text element onto the script in the inspector. your ontrigger stuff will work and all your scripts can youse one text element other wise you would need a lot of text elements on one canvas you wouldn't be able to reuse it.
     
  5. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Ah, this works, thanks, I thought onGUI was still needed.
     
  6. shadow-river

    shadow-river

    Joined:
    May 29, 2013
    Posts:
    63
    nah not for the new UI. OnGUI was used to generate things solely through script but now you access the elements through script via the canvas so its not needed. as long as your script knows what its accessing your fine.
    sorry if my explanations don't make sense I'm still learning as well.

    that script is good because you can put it on 50+ objects and they will all use that one text element.
     
  7. shadow-river

    shadow-river

    Joined:
    May 29, 2013
    Posts:
    63
    update your script to this.

    Code (JavaScript):
    1. #pragma strict
    2. import UnityEngine.UI;
    3.  
    4. var textToUse = "Text";
    5. var text : Text;
    6.  
    7. function Start ()
    8. {
    9.     text.text = "";
    10. }
    11.  
    12. function OnTriggerEnter (theCollider : Collider)
    13. {
    14.     if (theCollider.tag == "Player")
    15.     {
    16.         text.text = "" + textToUse;
    17.     }
    18. }
    19. function OnTriggerExit (theCollider : Collider)
    20. {
    21.     if (theCollider.tag == "Player")
    22.         {
    23.            
    24.             text.text = "";
    25.         }
    26. }
    27.  
    28.  
    29.  
    now that I think about it its probably best not to use update to change the text component, probably doesn't matter but for this you don't actually need to use the update function because it can be done the same in Ontrigger enter.