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 ic3t3a2000 · Dec 14, 2012 at 07:37 AM · arrayrandom.range

Using an Array to spawn a random object

I am trying to spawn a random gameobject on an empty game object when i push my mouse button. I get the following error.

NullReferenceException: Object reference not set to an instance of an object (wrapper stelemref) object:stelemref (object,intptr,object) RandomPiece.Update () (at Assets/Scripts/RandomPiece.js:17)

I can't figure out what I am doing wrong. Below is my code.

var respawnPrefab : GameObject; var respawnPrefab1 : GameObject; var respawnPrefab2 : GameObject; var respawn : GameObject; var loopHandle : boolean = true;

function Start() { if (respawn==null) respawn = GameObject.FindWithTag ("Respawn"); }

function Update() { var objs : GameObject[];

objs[0] = respawnPrefab; objs[1] = respawnPrefab1; objs[2] = respawnPrefab2;

if (Input.GetKeyDown (KeyCode.Mouse0)) {
Instantiate(objs[(Random.Range(0, objs.Length))], respawn.transform.position, respawn.transform.rotation); } }

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Tourist · Dec 14, 2012 at 10:27 AM

You have a null variable.

Go to the line 17 in your script (because : (at Assets/Scripts/RandomPiece.js:17)) and then check all variables before using them.

Assuming the line 17 is : Instantiate(objs[(Random.Range(0, objs.Length))], respawn.transform.position, respawn.transform.rotation);

then check 'respawnPrefab', 'respawnPrefab1', 'respawnPrefab2' and 'respawn' are not null like this :

 var iRandomIndex = Random.Range(0, objs.Length);
 if(objs[iRandomIndex] == null)
 {
 Debug.LogError("The prefab is not referenced at index " + iRandomIndex); 
 }
 else if(respawn == null)
 {
 Debug.LogError("There is no respawn gameobject in this scene.");
 }
 else
 {
 Instantiate(objs[iRandomIndex], respawn.transform.position, respawn.transform.rotation);
 }
Comment
Add comment · Show 4 · 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 ic3t3a2000 · Dec 14, 2012 at 03:22 PM 0
Share

I think I am declaring items in the array incorrectly but I am so new to program$$anonymous$$g that I can't say where I am wrong. Line 17 is were I am trying to link items in my array to the array I named objs.

Below are lines 16-19 where I am trying to fill my array with game objects.

  • var objs : GameObject[];

  • objs[0] = respawnPrefab;

  • objs[1] = respawnPrefab1;

  • objs[2] = respawnPrefab2;

avatar image Tourist · Dec 14, 2012 at 03:47 PM 0
Share

Here is a documentation on how to use arrays in javascript : http://docs.unity3d.com/Documentation/ScriptReference/Array.html

Your variable objs is null. var objs = new GameObject[3];

avatar image ic3t3a2000 · Dec 15, 2012 at 02:17 AM 0
Share

That did the trick!!! thanks so much!

avatar image Tourist · Dec 15, 2012 at 11:38 AM 0
Share

don't forget to mark the answer and close the thread

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

11 People are following this question.

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

Related Questions

Getting Vector3 from random item in List 1 Answer

How do i prevent an object instantiating another object straight away 2 Answers

How to randomly pick a gameobject except for one gameobject in an array ? 1 Answer

Assign role randomly from array 2 Answers

Array choosing more than one random choice in Start function 0 Answers


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