- Home /
Array with Gameobjects code
I've been trying to solve this thing for about an hour now, could somebody tell me why this error:
NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args)
UnityScript.Lang.UnityRuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args, System.Type scriptBaseType)
GUI [ Manager].Update () (at Assets/GUI [ Manager].js:15)
is happening with this code? :
var timer: float = 300; // set duration time in seconds in the Inspector
var cube: GameObject;
var cubeArray;
function Start() {
var cubeArray = new Array();
}
function Update(){
timer -= Time.deltaTime;
if (timer < 0){
timer = 5;
cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.AddComponent("Cube");
cube.transform.position = Vector3 (0, 30, 0);
cubeArray.Add( cube );
for (var i = 0; i < cubeArray.Length; i++) {
cubeArray[i].Fall();
}
} else {
guiText.text = timer.ToString("F1");
}
}
"Cube" is a script, if anyone is wondering. Thanks.
In Start() you have 'var cubeArray' so you are initializing a local variable rather than the top-level cubeArray. Remove the 'var'.
Sure didn't. I converted my answer into a comment. I don't use the Array class, and unfortunately I don't have time now to look into it.
Answer by Eric5h5 · May 31, 2013 at 08:19 PM
Don't use the Array class. Always use built-in arrays or generic Lists instead. The only things Array has by comparison are casting problems, slower code, and fewer features. So forget it exists. Also never do things like var cubeArray;
. You must always define the type, either explicitly, or implicitly by supplying a value.
I have changed my code, but it still gives the error: NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cache$$anonymous$$eyName, System.Type[] cache$$anonymous$$eyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cache$$anonymous$$eyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args) UnityScript.Lang.UnityRuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args, System.Type scriptBaseType) GUI [ $$anonymous$$anager].Update () (at Assets/GUI [ $$anonymous$$anager].js:16)
with the code: import System.Collections.Generic;
var timer: float = 300; // set duration time in seconds in the Inspector
var cubeArray;
function Start() {
var cubeArray = new List.<GameObject>();
}
function Update(){
timer -= Time.deltaTime;
if (timer < 0){
timer = 5;
cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.AddComponent("Cube");
cube.transform.position = Vector3 (0, 30, 0);
cubeArray.Add(cube);
for (selectedCube in ArrayList) {
}
} else {
guiText.text = timer.ToString("F1");
}
}
You haven't initialized cubeArray. You created a new local cubeArray in Start, but you never use it for anything. Also, like I said, you can't use "var cubeArray"; you must always declare the type explicitly or implicitly. Additionally, I don't know what "`for (selectedCube in ArrayList)`" is supposed to be, but it wouldn't work since ArrayList is a type. While "`cube.AddComponent("Cube");`" would technically work, you should generally not use the string version.
Thank you, that answered my question and my code is now working.