2

I'm writing a 3D maze program in C# and I need to have UI Text display "You Win!" When the player reaches the end of the maze.

I have a trigger set up in Unity as a cube, named FinishLine, and I have the UI text named winText

I'm getting an error on this line..

GUI.Box(New rect (10,10,100,90), winText);

the error is "The best overloaded method matfch for unityengine.gui.box (unityEngine rect, string)' has some invalid arguments

I also have no idea what those numbers are (10,10,100,90), so maybe that's messing something up? What are those values indicating...?

Here is my code..

public class TextTrigger : MonoBehaviour {

     public GUIText winText;
     private bool FinishLine = false;

     void Start () {
         FinishLine = false;
     }

     void OnTriggerEnter(Collider col){
         if (col.tag == "Player") {
             FinishLine = true;   
         }
     }

     void OnGui() {
         GUI.Box(new Rect(10,10,100,90), winText);
     }
 }

EDIT - Updated my code, and I have a new error. On line 21 it says: "UnityEngine.Texture does not contain a definition for text and no extension method 'text' accepting a first argument of type 'UnityEngine.Texture' could be found. Are you missing a using directive or an assembly refrence?

NEW CODE:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class FinishLine : MonoBehaviour {

public Texture winText;     private bool FinishPlane = false;

// Use this for initialization  void Start () {         FinishPlane =

false;

}

void OnTriggerEnter(Collider col)   {       if (col.tag == "Player") {
        FinishPlane = true;             winText.text = "You Win!";      }   } }
Jason Shaft
  • 65
  • 1
  • 2
  • 8
  • You might want to check out [the Game Development SE](http://gamedev.stackexchange.com/) for questions related to the Unity engine or video game building related questions. –  Feb 27 '17 at 04:39
  • Can you post the contents of your error? Also, have you considered using the new Unity UI? It's a lot simpler to manage - you can just create a Text component on an object, then enable/disable it to display the text as needed like any other GameObject/component. – Serlite Feb 27 '17 at 04:40
  • Opps, knew i was forgetting something. "The best overloaded method matfch for unityengine.gui.box (unityEngine rect, string)' has some invalid arguments – Jason Shaft Feb 27 '17 at 04:41

1 Answers1

2

First of all, it is OnGUI not OnGui. The spelling counts. If you find yourself using OnGUI, stop and find other ways to accomplish whatever you are doing.

GUIText is a legacy UI Component. It's old and the Text component should now be used. If you still want to use it, below is the proper way to use GUIText.

public GUIText winText;
private bool FinishLine = false;

void Start()
{
    FinishLine = false;
}

void OnTriggerEnter(Collider col)
{
    if (col.tag == "Player")
    {
        FinishLine = true;
        winText.text = "You Win";
    }
}

Text component should be used for this and below is how to do that with the Text component:

public Text winText;
private bool FinishLine = false;

void Start()
{
    FinishLine = false;
}

void OnTriggerEnter(Collider col)
{
    if (col.tag == "Player")
    {
        FinishLine = true;
        winText.text = "You Win";
    }
}

You can learn more about Unity's new UI here.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Thank you Programmer! I watched a couple videos, trying to through the code in now and im running into one error on line 20 "unityEngine.Texture' does not contain a definition for 'text' and no extension method text accepting a first argument of type 'unityEngine.texture' could be found are you missing a directive or an assembly refrence? – Jason Shaft Feb 27 '17 at 05:17
  • Put **EDIT** in your question and post the new code that is causing the error. I will take a look at it. Also, you are supposed to have `using UnityEngine.UI;` at the top of the code. – Programmer Feb 27 '17 at 06:01
  • It is `public Text winText;` not `public Texture winText;`. Please copy the code from the answer I left **directly**. – Programmer Feb 27 '17 at 19:15
  • ahp. I fixed it right before i saw your comment. I knew it had to be something little, I'm using monodev and it automatically put texture when i tpyed text. Thanks a lot man! – Jason Shaft Feb 27 '17 at 19:19
  • Nice. Please [accept](http://meta.stackexchange.com/a/5235) answer if your problem is solved. – Programmer Feb 27 '17 at 19:21
  • Lsat question, Should I have this script by it's self or add it into my PlayerControler? I have it by its self, and i connected it to my plane, and added theUI text into the Win Text spot in my inspector but it's not showing up when I collide with my object. – Jason Shaft Feb 27 '17 at 19:26
  • If text does not change then check if `OnTriggerEnter` is being called at-all. I suggest you ask a new question for this. Before you ask a question, put Debug.Log` in the `OnTriggerEnter` function then put another one inside the `if (col.tag == "Player")`. You can use this to figure out what the problem is. If you still can't then post a new question and tell us what the `Debug.Log` says. – Programmer Feb 27 '17 at 19:34