well… there are several spots that are making this come up, but essentially your problem lies in lines like these:
spawnpoints;
spawnpoint;
newTrans;
spawnpoints, spawnpoint, and newTrans are all variables. By writing “spawnpoint;” you are basically saying “hey unity… here’s a Transform.” The error message you’re getting from Unity is basically saying “Yeah, that’s nice, what do you want to do with it?”
In addition to this, you’re technically defining the variables only when you hit the “Builder” button, which technically means that the variables don’t exist when you hit the “Cook” or “Hunter” buttons. Javascript on PC/Mac will assist you with this problem, but it’s still good practice to define your variables outside of any curly brackets that are referencing them.
I’ve made this change along with several other comments here, hopefully this helps:
function OnGUI() {
//I'm assuming you have cont and Skin defined in your version of the script
//It would be better to handle your length check like this:
if (Skin.Length > 0) {
GUI.skin = Skin[cont%Skin.Length];
} else {
Debug.LogError("Assign at least 1 sking on the array");
return;
}
//Defining these variables outside of the if-statements that use them
var spawnpoints : GameObject[];
var spawnpoint : Transform;
var newTrans : Transform;
if (GUI.Button(Rect(600,300,300,60),"Builder")) {
//You're fining all objects tagged "Spawnpoint"
spawnpoints = GameObject.FindGameObjectsWithTag ("Spawnpoint");
Debug.Log("spawns: "+spawnpoints.length);
//You're setting spawn point to the Transform of a randomly chosen item from spawnpoints
spawnpoint = spawnpoints[Random.Range(0, spawnpoints.length)].transform;
//You're instantiating a builder across the network at the spawnposition's position an rotation an putting it in group 0
newTrans = Network.Instantiate(builder,spawnpoint.position, spawnpoint.rotation, 0) as Transform;
//Presumably you're telling the game to display a string called name that I will assume you have defined in your actual script
chatScript.addGameChatMessage(name+" is a Builder");
//You are destroying this script component from the object it's attached to
Destroy (this);
}
if (GUI.Button(Rect(600,200,300,60),"Cook")) {
//These three lines are doing nothing
//You're basically saying "hey, here's a variable"
//and Unity is saying "Yeah? And you're telling me this because? What do you want to use them for?"
spawnpoints;
spawnpoint;
newTrans;
//Presumably you're telling the game to display a string called name that I will assume you have defined in your actual script
chatScript.addGameChatMessage(name+" is a Cook");
//You are destroying this script component from the object it's attached to
Destroy (this);
}
if (GUI.Button(Rect(600,100,300,60),"Hunter")) {
//Same as above:
//These three lines are doing nothing
//You're basically saying "hey, here's a variable"
//and Unity is saying "Yeah? And you're telling me this because? What do you want to use them for?"
spawnpoints;
spawnpoint;
newTrans;
//Presumably you're telling the game to display a string called name that I will assume you have defined in your actual script
chatScript.addGameChatMessage(name+" is a Hunter");
//You are destroying this script component from the object it's attached to
Destroy (this);
}
}