- Home /
The question is answered, right answer was accepted
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:
in the switch statement
in the first "if" statement (both variables in condition)
in both "else if" statements (all three variables in condition)
at "group.SnakeGroup.goGone" (both times)
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;
}
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.
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).
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.