Can not use variable in GUI

My gui need to use a variable for text but the code gives me

“Assets/NPC_Chat.js(36,32): BCE0004: Ambiguous reference ‘Button’: UnityEngine.GUI.Button(UnityEngine.Rect, UnityEngine.GUIContent), UnityEngine.GUI.Button(UnityEngine.Rect, UnityEngine.Texture), UnityEngine.GUI.Button(UnityEngine.Rect, String).”

Here is the code

#pragma strict
var NPC_Text;
var Talking;

function Start () {

}

function Update () 
{


}

function OnTriggerEnter (other : Collider)
	{
		if(other.gameObject.name == "Player")
		{
			Chat ("Soldier");
			Talking = true;
		}
	}

function Chat (NPC)
{
	if(NPC == "Soldier")
	{
		NPC_Text = "Would you like to buy something?";
	}
}

function OnGUI ()
{
	if(Talking)
	{
		if (GUI.Button (new Rect (10,70,150,50), NPC_Text  ))
		{
		
		}
	}
}

The reason in fact is that the variable is not initialized, you should remember that in C# you can´t use a variable that is not initialized.

1 Answer

1

Declare like this:

var NPC_Text:String="";