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 /
avatar image
0
Question by Blackace · Nov 21, 2011 at 12:05 PM · randomarraysobjects

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);
 
 }
Comment
Add comment · Show 2
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 Kacer · Nov 21, 2011 at 12:44 PM 1
Share

You might want to format that code properly, its really hard to read.

avatar image Blackace · Nov 21, 2011 at 01:22 PM 0
Share

$$anonymous$$aybe like that? Sorry I see it the same way hope now is fixed for all.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

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 Blackace · Nov 22, 2011 at 07:32 AM 0
Share

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 :\

avatar image aldonaletto · Nov 22, 2011 at 10:10 AM 0
Share

@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.

avatar image
0

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 :)

Comment
Add comment · Show 3 · 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 SkaredCreations · Nov 22, 2011 at 05:55 PM 0
Share

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.

avatar image Blackace · Nov 23, 2011 at 07:33 AM 0
Share

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 :)

avatar image SkaredCreations · Nov 23, 2011 at 09:06 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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

Instantiate a random item at a random position 1 Answer


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