- Home /
Cannot use Static var/ Unknown Idientifier
Greetings,
I'm trying to use a static var, but unity tells me it's a Unknown Identifier. My other static var works perfectly, exception this one.
Here where's the bug
if(other.tag == "Player"){
doorSpawn = goToDoorNum;
Application.LoadLevel("L"+gameLevel+"R"+goToRoomNum);
}
gameLevel and doorSpawn are both static var declared here in another script:
//At which door does the character spawn.
static var doorSpawn : int;
//Which game level are we in.
static var gameLevel : int;
But only gameLevel return me an error. Any idea on how to force Unity to compile it correctly?
Edit:
Script A:
//At which door does the character spawn.
static var doorSpawn : int;
//Which game level are we in.
static var gameLevel : int;
//In which room the character is.
private var charInRoom : int;
//In which room the boss is.
private var bossInRoom : int;
//How many rooms there is.
var roomsQty : int;
private var enemiesInRoom : int[];
function Awake(){
//Resize a native array.
enemiesInRoom = new int[roomsQty+1];
DontDestroyOnLoad (transform.gameObject);
print(enemiesInRoom.Length);
}
function Update () {
}
function charRoomUpdate(room : int){
charInRoom = room;
}
function spawnCharacter(){
if(gameControl.isPlaying){
var DoorSpawn : GameObject;
GameObject.Find("Door"+doorSpawn;)
SendMessage.DoorSpawn(spawnCharacterDoor);
}
}
Script B:
//The number of the door.
var doorNum : int;
gameObject.name = "Door"+doorNum;
;
//The room number to which the character will go.
var goToRoomNum : int;
//The door number the character should spawn at in the next room.
var goToDoorNum : int;
function OnTriggerEnter(other : Collider){
if(other.tag == "Player"){
doorSpawn = goToDoorNum;
Application.LoadLevel("L"+levelManager.gameLevel+"R"+goToRoomNum);
}
}
function spawnCharacterDoor(){
SpawnPoint : GameObject;
GameObject.Find(gameObject.name+"/Spawn");
}
Answer by CHPedersen · Jan 09, 2012 at 02:53 PM
You can't force Unity to compile something correctly, but it can force you to code correctly. ;-)
That a variable is static doesn't mean it can be referenced anywhere without including the name of the class in which the variable is declared - it means that only one instance of the variable exists across your project, and that you can reference it without an instance (an object) of the class in which the variable is declared.
This means that if, in ScriptA, you have written,
static var doorSpawn : int;
Then, in ScriptB, accessing the variable through "ScriptA.doorSpawn
" is correct, but just writing doorSpawn
is not. If your compiler doesn't give you an error with doorSpawn, it is because another local variable which is not the static one, but which has the same name AS the static one, exists in the first script.
Then why does 'doorSpawn = goToDoorNum;' works? In the inspector, if I start typing 'doorSpawn' it even show me that's it available. It's only gameLevel that does not work, and both have been declared the same way and at the same time.
You need to read my reply again. I told you already:
"If your compiler doesn't give you an error with doorSpawn, it is because another local variable which is not the static one, but which has the same name AS the static one, exists in the first script."
I've edited with the 2 full scripts for your convenience. As you said, referencing the script DID worked.
I did read what you said, but what I'm still puzzled about, is at why it works with doorSpawn. If you read the scripts thoroughly, I declare 'doorSpawn' in Script A modify it from script B. But those are the 2 only places 'doorSpawn' is ever mentioned.
So I don't see how I mention in locally in scriptB...
Thanks for posting the scripts. I don't see where you declare the local variable either, which leaves me a little puzzled, to be honest, but what I said is what the compiler's error message means. If the compiler recognizes a variable, it's because that variable is available in the current scope. And since you didn't access the static one correctly, there is no other explanation than the one I provided - a variable with that name must've been somehow locally within scope. Are you using an external editor? It's possible Unity's compiler was displaying error messages from a previous version of the script where a variable with that name was in scope, because changes made to the script in the external compiler hadn't been saved yet. That sometimes occurs to me. Other than that, I'm left to guesswork, same as you. But I'm glad you got it working, in any case. :P
Oh well, mysteries of compiling... 'Cause I'm using Unity's $$anonymous$$onoDevelop... At least it work.
Your answer
Follow this Question
Related Questions
JScript problem 1 Answer
Invalid IL code error when calling static function 1 Answer
Accessing a js static var from a c# script 1 Answer
Javascript Compile error 2 Answers
passing variables through scripts 0 Answers