Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Oninji · Jan 09, 2012 at 02:39 PM · javascripterrorvariablesstaticcompile

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");
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Oninji · Jan 09, 2012 at 02:59 PM 0
Share

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.

avatar image CHPedersen · Jan 09, 2012 at 03:02 PM 0
Share

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."

avatar image Oninji · Jan 09, 2012 at 03:25 PM 0
Share

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...

avatar image CHPedersen · Jan 09, 2012 at 07:03 PM 0
Share

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

avatar image Oninji · Jan 10, 2012 at 05:13 PM 0
Share

Oh well, mysteries of compiling... 'Cause I'm using Unity's $$anonymous$$onoDevelop... At least it work.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges