Displaying variables using gui in Unity

2019-08-12 03:53发布

In my game, I want a button that when clicked on, overwrites a string. This string will then be displayed in some text above. So far it is going very wrong...

Here is the code ( in C#) that I use:

using UnityEngine;
using System.Collections;

public class ConnectGUI : MonoBehaviour {
private string map = "No map selected.";

// Use this for initialization
void Start () {

}
void OnGUI () {
    GUI.Label(new Rect(10,10,200,90), "Map selected: ", map);
    if(GUI.Button (new Rect(10,50,90,20), "Pier")){
        map = "Pier";
        }
    }

// Update is called once per frame
void Update () {

}
}

Any ideas?

1条回答
Explosion°爆炸
2楼-- · 2019-08-12 04:02

You've made a mistake in following line:

GUI.Label(new Rect(10,10,200,90), "Map selected: ", map);

should be

GUI.Label(new Rect(10,10,200,90), "Map selected: " + map);

Change the comma , to a plus +;

查看更多
登录 后发表回答