Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Beta_Artist · Sep 30, 2011 at 11:51 PM · javascriptarraycontrollerspawnpoints

working with array index...

I've been getting an error message telling me that:

 IndexOutOfRangeException: Array index is out of range.

I tried playing with the int variable for Random.Range to get my three spawn points into "range" with nothing working.

The Code:

 var spawn : Transform[]; 
 var game_cube : Rigidbody;
 var cube_count = 0;
 
 InvokeRepeating("LaunchProjectile", 2, 3);
 function Update() {
     if (cube_count>= 10) {
         CancelInvoke();
     }
 
 }
 
 function LaunchProjectile () {
     var randomPick : int = Random.Range(0,2);
 
     instance = Instantiate(game_cube, spawn[randomPick].position, transform.rotation);
     instance.velocity = Vector3.zero;
     cube_count = cube_count +1;
 
 }

Does it have something to do with the "Instantiate" script? Or do I have to attach this directly to the spawn points? The error is telling me there is something wrong with the "instance = Instantiate(game_cube, spawn[randomPick].position, transform.rotation);" line of code. Any ideas?

Comment
Add comment · Show 1
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 Julien-Lynge · Sep 30, 2011 at 11:59 PM 1
Share

Have you attached at least 3 spawn points to your Transform[] array? What the error means is that you're asking for, say, spawn[1] but you only have one item in the array (remember that index starts at zero). Try adding a debug statement:

Debug.Log("Spawn has " + spawn.Length + " members, I'm requesting member " + (randomPick + 1));

on the line directly above your instantiate line.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Oct 01, 2011 at 12:14 AM

One possible reason for these errors that seem to be cursing you maybe a second forgotten instance of this script attached to some object in your scene - you set the spawn array in one instance, but there's another one hidden somewhere complaining that its spawn array is empty.
Setting Random.Range max to spawn.length is the right thing to do: you can use how many spawn points you want, and additionally this will solve the out of range problem, no matter how many lost instances are in your scene (changes in the script instructions affect all instances automatically).

function LaunchProjectile () {
    var randomPick : int = Random.Range(0,spawn.length);
    ...
Just to check the basics: remember to set the spawn size to 3 and fill the 3 elements with your spawn point objects.
Comment
Add comment · Show 1 · 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 Beta_Artist · Oct 03, 2011 at 06:12 PM 0
Share

how would I setup the array for gameObjects?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Resizing arrays, and what is the default array type anyway? 3 Answers

array out of range in simple pathinder javascript 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