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
  4. Dismiss Notice

How to display text after collision?

Discussion in 'Scripting' started by Kevinpptx, Feb 29, 2020.

  1. Kevinpptx

    Kevinpptx

    Joined:
    Nov 30, 2019
    Posts:
    11
    How can I display a Text after I collided with a GameObject? I tried this:


    Code (CSharp):
    1.    void OnGUI()
    2.         {
    3.             guiStyle.fontSize = 66;
    4.             guiStyle.fixedHeight = 66;
    5.             guiStyle.fixedWidth = 66;
    6.             guiStyle.normal.textColor = Color.white;
    7.  
    8.      
    9.             GUI.Label(new Rect(10, 10, 100, 20), "Score: " + score, guiStyle);
    10.  
    11.             guiStyle1.fontSize = 200;
    12.             guiStyle1.fixedHeight = 66;
    13.             guiStyle1.fixedWidth = 66;
    14.             guiStyle1.normal.textColor = Color.white;
    15.         }
    16.  
    17.         private void OnTriggerEnter2D(Collider2D collision)
    18.         {
    19.             GUI.Label(new Rect(10, 10, 100, 20), "Score: " + score, guiStyle1);
    20.         }
    21.     }
     
  2. akadird

    akadird

    Joined:
    Dec 19, 2017
    Posts:
    11
    i never create a UI by script. For something like this, create a canvas and Text item in the editr so u can set active or instantiate everytime u collide.
    i dont have much of experience so there might be better ways though.
     
  3. Kevinpptx

    Kevinpptx

    Joined:
    Nov 30, 2019
    Posts:
    11
    I have to create it via script because it has to display "score".