- Home /
Variable not Assigned
Hi there,
I have a problem with a variable. Unity says its not assigned, but I have assigned in the inspector. In my opinion everything is correct. I don't see a mistake. I have tried everythng: reassigning the variable, renaming the variable - nothing works. Anyone an Idea? Here is my code - maybe there is the mistake:
 var cubePrefab : Transform;
 var numberOfCubes :int =10; //number of Cubes needed in this level
 
 function Start()
 {
     var cube = new Array();
     for(var i=0;i<numberOfCubes; i++)
     {
         Instantiate(cubePrefab, Vector3 (2*i, 0.5, 0), Quaternion.identity);
     }
     //cube[5].Transform.translate(0,2,0);
 }
Are you assigning it to the default value in the script or to an instance of the script on a GameObject?
Is this line:
cube[5].Transform.translate(0,2,0);
The line that is throwing the error? If so - you never assign anything to the cube array. Change the code to:
var cube = new Array();
for(var i=0;i
{
cube.Push( Instantiate(cubePrefab, Vector3 (2*i, 0.5, 0), Quaternion.identity) );
}
Answer 1: The script is attached to the camera (only) and I assigned the prefab to the cubePrefab variable in the Inspector.
Answer 2: no, that line is not throwing the error, because it is marked as a comment. I went a step back, that's why I made it a comment. The Instanciate(...); Line is throwing the error without taking the array into play.
Which variable Unity says it's unassigned? A common reason for this error is a duplicate instance of the script assigned to some other object by mistake - you set the variables in the "official" instance, but the forgotten one keeps throwing errors.
Just a word of advice (not to step on any toes) Dont use Array(). If you are com$$anonymous$$g from AS or Javascript it is natural to want to use the old fashion array, in reality a builtin array(fixed size unity array) is the way to go. If you need to access to resizing you can use a List, i will show both below.
 //Fixed Size Transform Array (can only contain the declared type, any type you choose)
 var cube:Transform[]=new Transform[numberOfCubes];
 
 //LIST 
 //At top of script
 import System.Collections.Generic;
 
 //Start
 //Resizable(same as Array, but much faster!) 
 var cube:List.<Transform> = new List.<Transform>();
Answer by fschaar · Jul 06, 2012 at 06:36 AM
Ok, thanks for the Answers, I think I have a solution now!
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                