- Home /
Scripts accessing one another (JS)
I have looked at a ton of similar questions for hours today, and I cannot figure out why my problem is different, or what detail I'm missing.
I have a script for each of several players to select a character, and I simply want to make sure no one chooses the same character. Please excuse how verbose it is. I know I have extra stuff in here - it's been mangled throughout the day, but what I really need is what's giving me this specific error.
I have a variable:
public static var P1Selected = null;
This holds the choice this player has made (an int).
This is used to get the object, and a reference to the script.
Player2 = GameObject.Find("Player2");
P2script = Player2.GetComponent(P2Select);
I also have a get method.
public function getSelected(){
return P1Selected;
}
This is to allow other scripts to inquire the value. I have also simply tried accessing the variable, but I find the same problem.
Finally, this is the line where I test.
if(P2script.getSelected() != CharIndex &&
P3script.getSelected() != CharIndex &&
P4script.getSelected() != CharIndex){
//successfully lock in character
}
It continuously tells me that the variable and the function do not exist in the other script. I have also tried declaring it as
P2script : P2Select = Player2.GetComponent(P2Select);
To which the compiler responds "unexpected token". Saw that in some other examples. I also saw people mentioning dropping one script in standard assets, because it compiles first. This does not help, as they'll be changing at runtime and also need to access each other.
I feel like I've missed something small and need another pair of eyes to spot an obvious mistake.
It is unclear how all your variables are defined. It might be easier to figure out if you put all these scripts on PasteBin and provided a reference. Note that since 'P1Selected' is a static variable, you will have only a single instance shared between all the script, and you can access it by the Class (no need for GetComponent(). Not sure if that is the way it needs to be structured or not.
Not sure how everything ties together. Try posting complete classes. Feel free to strip everything out that doesn't touch the mentioned variables.
You should also consider using an array ins$$anonymous$$d of hardcoding the player numbers.
You have four essentially identical scripts called P1Select, P2Select, P3Select, P4Select ?
Your answer
Follow this Question
Related Questions
Acces to a var from another Script. 2 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Problem with PlayerHealth and renderer 1 Answer
variable = true from another script 2 Answers