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 AngryOldMan · Feb 28, 2011 at 11:09 PM · arrayruntimeexceptionarray-out-of-range-exceptoutofbounds

Instantiating random prefabs

Hello, We are trying to make a random pickup appear in a random place. i think i have the sort of right lines but it keeps giving us an error message so it's definatly not right lol. This is what we have so far.

var prefabOnLevel : boolean = false; var nextSpawnTime : float = 2.0; var timeBetweenSpawns : float = 30.0;

function Awake() { var SpawnWhat : GameObject[] = GameObject.FindGameObjectsWithTag ("PickUp"); var SpawnThis : GameObject = SpawnWhat[Random.Range(0,SpawnWhat.length)]; var PickUpSpawn : GameObject[] = GameObject.FindGameObjectsWithTag ("PickUpSpawn"); var SpawnHere : Transform = PickUpSpawn[Random.Range(0, PickUpSpawn.length)].transform; }

function Update () { if (!GameObject.FindWithTag ("PickUp")) { prefabOnLevel = false; }

 if (nextSpawnTime < Time.time)
 {
     nextSpawnTime = Time.time + timeBetweenSpawns;

     if (prefabOnLevel == false)
     {
         Instantiate (SpawnThis, SpawnHere.position, SpawnHere.rotation);
         Debug.Log ("should be a pick up in level");
     }
 }

}

This particular format gives us the error of Unknown variable on the Instantiate line.

function Update () { if (!GameObject.FindWithTag ("PickUp")) { prefabOnLevel = false; }

 if (nextSpawnTime < Time.time)
 {
     nextSpawnTime = Time.time + timeBetweenSpawns;

     if (prefabOnLevel == false)
     {
         var SpawnWhat : GameObject[] = GameObject.FindGameObjectsWithTag ("PickUp");
         var SpawnThis : GameObject = SpawnWhat[Random.Range(0,SpawnWhat.length)];
         var PickUpSpawn : GameObject[] = GameObject.FindGameObjectsWithTag ("PickUpSpawn");
         var SpawnHere : Transform = PickUpSpawn[Random.Range(0, PickUpSpawn.length)].transform;
         Instantiate (SpawnThis, SpawnHere.position, SpawnHere.rotation);
         Debug.Log ("should be a pick up in level");
     }
 }

}

this format gives us the error of "Array index is out of range" at runtime, in the line var SpawnThis. What are we doing wrong?

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
2
Best Answer

Answer by DaveA · Feb 28, 2011 at 11:25 PM

In the first, your variables are local to Awake, you'd need to put them outside.

In the second, Random.Range is inclusive, so if the length were, say, 5, it might return 5, which would be out of bounds. Try Random.Range(0,PickUpSpawn.length-1)

Comment
Add comment · Show 6 · 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 AngryOldMan · Feb 28, 2011 at 11:39 PM 0
Share

Iv tried the second option with Random.Range(0,PickUpSpawn.length-1) and it still brings back "Array index is out of range" at runtime. Thanks for your response, could you offer any more insight into the problem? Is there any more information I can provide you with?

avatar image DaveA · Feb 28, 2011 at 11:39 PM 0
Share

@Alec - Jinx!!!

avatar image DaveA · Feb 28, 2011 at 11:41 PM 1
Share

In that case, I'd hit the debugger and/or put in some Debug.Log to see that all things are what I expect. I suspect the 'Find' in the preceding line is not finding anything.

avatar image DaveA · Feb 28, 2011 at 11:42 PM 1
Share

Also, if you are creating those pickups with a script, it would be more efficient to keep track of those (in a list or array), rather than use Find because finding is slow.

avatar image Alec-Slayden · Feb 28, 2011 at 11:55 PM 2
Share

lol nice :D Bob, also make sure you're changing both your random ranges to be -1 to the length, or you'll still get the out of range for your SpawnHere

Show more comments

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

1 Person is following this question.

avatar image

Related Questions

IndexOutOfRange error on iPhone, but not on PC/Player 0 Answers

Class Array referencing 1 Answer

Can't Re-Enable Lightmaps after Disabling Them at Runtime 1 Answer

Enemy ai's won't spawn in scene / IndexOutOfRangeException: Array index is out of range. EnemiesSpawner.Spawn () (at Assets/EnemiesSpawner.cs:17) 0 Answers

Find the average of 10 Vectors 4 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