- Home /
playerID does not exist in current context?
void Awake () { players = new GameObject[numPlayers];
Player PlayerScript;
PlayerScript = players[playerID].GetComponent<Player>();
PlayerScript.playerID = playerID;
}
not sure why this isnt working, as a playerID variable is in the Player script.
Answer by DiegoSLTS · Feb 18, 2015 at 12:42 PM
playerID is just a variable on PlayerScript? You're using a playerID variable too that should be defined on the same script.
What line gives you the error?
PlayerScript = players[playerID].GetComponent<Player>();
or
PlayerScript.playerID = playerID;
?
PlayerID is just a variable from Player script i was trying to get, I got both lines in error, one for each PlayerID
You need to read more about scopes and how public variables work. A public variable doesn't mean you can write it's name anywhere, it could be a public instance variable (so from outside the class you need to have a reference to that instance and call instanceOfClass.playerID) or a public class variable (so you have to write ClassName.playerID).
In your case you're just writing "playerID", so the compiler assumes it's a public variable of the same script, it won't use the public variable defined in other classes.
You need a variable on that script (NOT PlayerScript) to be able to write those lines, and from the little code you posted it looks like that's what you actually wanted.
Some questions that might help you understand the problem in your code: What value should playerID have when Awake is called? How is that value set?
Answer by alok-kr-029 · Feb 19, 2015 at 12:45 PM
Try debugging this then proceed further
Player PlayerScript = players[playerID].GetComponent<Player>();
Debug.Log(PlayerScript.playerID);
Answer by giulio-pierucci · Feb 18, 2015 at 10:22 AM
Player class must have a public field playerID
public int playerID = 0;
It does have a public field, but in the Player script "public int playerID; " Should it be in the gamemanager ins$$anonymous$$d/aswell?
Your answer
Follow this Question
Related Questions
Issues accessing a variable in another script 0 Answers
Cannot implicitly convert type `void' to `float' 1 Answer
Getting variable from another script 2 Answers
Function in variable (UnityScript) 1 Answer
GetComponent from class 1 Answer