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 /
This question was closed Jun 11, 2012 at 11:20 PM by You! for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by You! · Jun 06, 2012 at 10:50 PM · variablenoobreference-other-objectunknown-identifier

Unknown Identifiers for declared variables?

I am having problems with variables that, although I am declaring them, I am getting an "Unknown Identifier" error. The errors are in this code:

 enum type {runner,attacker,circler,wanderer}
 
 function Start ()
 {
     var spawner  = transform.parent.gameObject;
     var group  = spawner.SnakeSpawn.group;
     var gravity : float = spawner.SnakeSpawn.gravity;
     var speed : float = spawner.SnakeSpawn.speed;
     var rotationSpeed : float = spawner.SnakeSpawn.rotationSpeed;
     var distance : float = spawner.SnakeSpawn.distance;
     var snakeType = spawner.SnakeSpawn.type;
 }
 
 function Update () {
 
     switch(snakeType)
 
         {
                 //[...]
         }
     
     
 }
 function OnCollisionEnter ()
 {
     if ((spawner.SnakeSpawn.color) == (group.SnakeGroup.correct))
     {
         //Make door 'result' true
         group.SnakeGroup.goGone = true;
         Destroy(gameObject);
     }
     else if (spawner.SnakeSpawn.color != group.SnakeGroup.correct)
     {
         group.SnakeGroup.goGone = true;
         Destroy(gameObject);
     }
     else if (group.SnakeGroup.goGone)
     {
         Destroy(gameObject);
     }
 }

The errors are:

  1. in the switch statement

  2. in the first "if" statement (both variables in condition)

  3. in both "else if" statements (all three variables in condition)

  4. at "group.SnakeGroup.goGone" (both times)

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

2 Replies

  • Sort: 
avatar image
2
Best Answer

Answer by bompi88 · Jun 06, 2012 at 11:21 PM

You should declare all variables that will be used by two or more function "on top of the script", so they will be declared as "global" variables in that particular script. Then you could set the variables you want in the Start function. Example your spawn variable is declared in the Start function and cannot be read/or written to by other functions, you should to as follows (with all of your, must be used be others variables):

 var spawner : GameObject;
 
 function Start() {
   spawner = transform.parent.gameObject;
 }

bottomline: do as my example with all of your variables:

 var spawner : GameObject;
 var group;
 var gravity : float;
 var speed : float;
 var rotationSpeed : float;
 var distance : float;
 var snakeType;
 
 function Start ()
 {
     spawner  = transform.parent.gameObject;
     group  = spawner.SnakeSpawn.group;
     gravity = spawner.SnakeSpawn.gravity;
     speed = spawner.SnakeSpawn.speed;
     rotationSpeed = spawner.SnakeSpawn.rotationSpeed;
     distance = spawner.SnakeSpawn.distance;
     snakeType = spawner.SnakeSpawn.type;
 }
Comment
Add comment · Show 2 · 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 You! · Jun 06, 2012 at 11:24 PM 0
Share

Okay! That is exactly what I needed! Thank you!

avatar image bompi88 · Jun 06, 2012 at 11:43 PM 0
Share

you're welcome !

avatar image
2

Answer by Eric5h5 · Jun 06, 2012 at 11:08 PM

None of your variables are available outside the Start function, so they can only be used in the Start function. You have to declare variables outside functions if you want them to be available in other functions.

Comment
Add comment · Show 2 · 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 You! · Jun 06, 2012 at 11:21 PM 0
Share

Is there a way that I can declare them at start without them only being available within the function? The objects which are going to have this script are instantiated in the middle of the game, and parenting of this object changes in the middle of the game (at instantiation).

avatar image syclamoth · Jun 07, 2012 at 12:16 AM 0
Share

You can declare a variable without having to assign it, just keep in $$anonymous$$d that this can lead to Null Reference Exceptions in some cases.

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

Speed updated from another script, but doesn't persist 1 Answer

Collision object and access to a script variable? 1 Answer

Access class variables of selected object 1 Answer

Changing The Value Of A Variable From Another Script (JavaScript) 1 Answer

Trigger Script Not Firing 4 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