Unity GUIText on Collision c#

后端 未结 1 938
余生分开走
余生分开走 2021-01-22 15:25

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 c

相关标签:
1条回答
  • 2021-01-22 15:59

    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.

    0 讨论(0)
提交回复
热议问题