- Home /
Beginner : Random gameobject from array
Hi all, I'm a very very beginner user I'm studing tutorials and reading all easy stuff I find about scripting. I'm trying to do some simple things to better understand about scripting. Now I wanted to do something simple like make a plane and a cube appear in a random way for some seconds and then be destroyed. I reached to do with just one object but i'm facing problem with arrays I guess even if I've read about it and searched for days cannot find my error. Unity says Object reference not set to an instance of an object (line 14 : aObjects.Add(plane);) but I don't understand why. I attached the script to the main camera and set the cube and the plane in the scene as gameobject of the script in the main camera inspector. Any help would be higly appreciated. Thanks and sorry for my bad english and if it's too dumb question :\
var intervallo = 4;
private var prossimo = 0.0;
var aObjects = new Array();
var plane : GameObject;
plane = GameObject.Find("Plane");
var cube : GameObject;
cube = GameObject.Find("Cube");
aObjects.Add(plane);
aObjects.Add(cube);
function Update () {
if (Time.time > prossimo)
{
prossimo = Time.time + intervallo;
var clone = Instantiate(aObjects[Random.Range(0, aObjects.Length)], Vector3(0, 0, 0), Quaternion.Euler(270, 0, 0));
}
Destroy (clone, 3.0);
}
You might want to format that code properly, its really hard to read.
$$anonymous$$aybe like that? Sorry I see it the same way hope now is fixed for all.
Answer by kromenak · Nov 21, 2011 at 05:37 PM
There are two things in your code that could cause you to get an "Object Reference Not Set" problem.
The first is if the array object "aObjects" is not actually pointing to a valid array object. It looks like you may have forgotten to call "aObjects = new Array()". This will instantiate a new array instance, which you can then use to store objects. I'm a bit rusty with my Javascript, but I believe that saying "var aObjects : Array" is not enough.
Another reason you could get a problem like this is if GameObject.Find("Plane") is failing to find an object called "Plane" in your scene. Find() will return null if it can't find anything, so trying to Add(null) might cause some problems.
Thanks kromenak unfortunately I already tried both "aObjects = new Array()" and as I wrote, but nothing .. and for what concern the scene as I wrote there is an object called Plane (the standard one you can put in the editor) that's why I do not understand where I'm wrong. Anyway thank you very much for the answer I take your tip and I modify the line.
Now $$anonymous$$y error is always
NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (IConvertible convertible) Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (System.Object value) Boo.Lang.Runtime.RuntimeServices.UnboxInt32 (System.Object value) Selection.Update () (at Assets/Selection.js:23)
(Note that 23 is the line with the Instantiate function)
To be honest I do not understand a word of this error :\
@Blackace, please click the more button and convert this answer to a comment in the @kromenak's answer - the box Your answer should be used only to answer the question, and comments and replies should be added only with the button add new comment. Your other answer is ok, since it shows a solution for the question.
Unity Answers is different from forums, but this is not clearly explained and many newcomers make this same mistake.
Answer by Blackace · Nov 22, 2011 at 08:59 AM
Just solved.. But please could someone explain me why??
Changed var clone = Instantiate(aObjects[Random.Range(0, aObjects.Length)], Vector3(0, 0, 0), Quaternion.Euler(270, 0, 0)); to var clone = Instantiate(aObjects[Random.Range(0, 2)], Vector3(0, 0, 0), Quaternion.Euler(270, 0, 0));
and it works. Any explaination would be appreciated :)
You are using Javascript and not C#, so "Length" of your array object should be written in lowercase "length", may be it is why. Also I wonder where you are writing the first code (that before Update), it should be inside a function like Start and not written directly in the class code.
Also when you use Random.Range with integer numbers and not float, you should use the length+1 as max value because with ints the max value is exclusive and not inclusive.
yes ZioRed thanks, with aObjects.length it works! Not with aObjects.length+1 (as it give me an error). About the lines before Update well in all tutorials i did until now they always declare variables outside I understood that in this way you can use in more functions. I'm still learning (it's since a pair of week that I'm learning java) so I'm not always sure to write right things :D Anyway thank you very much :) I'm going to put the adding lines in the function start I guess is more correct and I already removed the find lines and did the drag and drop in the editor ...it should be ok now :)
Oh yes sorry, you're right, .length is correct in the Random.Range because the array and lists are zero-based, so length is already the last index+1 :) And about the question of the code inside the class, the variables that should be accessed from different functions inside the class they must be declared in the class code but only the declaration (and initialization, if any needed) but not the code/operations on that (that is, ok the declaration, but your .Add and GameObject.Find must be in the Start, as it seems you already did ;)
Your answer
Follow this Question
Related Questions
How do you get different random numbers for each object array? 1 Answer
Placing trees randomly across a large terrain 1 Answer
Serialization Error 1 Answer
Generating pickups randomly 3 Answers