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 Duplicis · Dec 23, 2011 at 10:50 PM · arrayspawnrespawnout of range

Array index out of Range

Hi im working on a round based Zombie shooter and it works fine. But if i want to open it in the webplayer nothing happens the only problem the Console shows is: IndexOutOfRangeException: Array index is out of range.

This is my Code:

var enemyPrefabs : Transform[]=new Transform[1]; var amountEnemies = 0; static var multiplicator=5; var lvl=1; var spawnPoints : Transform[]=new Transform[12];

function Update(){

multiplicator=Time.time*0.005+3; if(GameObject.Find("Enemy(Clone)")==false&&Time.time>5){ lvl+=1; amountEnemies+=5; Invoke("Up",0);

} else{ CancelInvoke();

}

} function Up() {

for (i=0; i < amountEnemies; i++) // How many enemies to instantiate total. { var obj : Transform=enemyPrefabs[Random.Range(0,enemyPrefabs.length-1)];// Randomize the different enemies to instantiate. var pos: Transform =spawnPoints[ Random.Range(0,spawnPoints.Length)]; // Randomize the spawnPoints to instantiate enemy at next. Instantiate(obj,pos.position,pos.rotation);

} } I have really no idea how the array could be out of range plss help me.

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 jahroy · Dec 23, 2011 at 11:10 PM 0
Share

Please format your code so it's readable.

I'd love to help.

1 Reply

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

Answer by jahroy · Dec 23, 2011 at 11:20 PM

It's very hard to read your code, but it looks like it has the potential to access a bad index in the spawnPoints array.

You properly limit the max index of your enemyPrefabs array to its length minus one, but you do not exercise the same wisdom when randomizing the index for the spawnPoints array.

Here is your code formatted and fixed:

var spawnPoints : Transform [] = new Transform [12]; var enemyPrefabs : Transform [] = new Transform [1];

var amountEnemies : int = 0; var levelNumber : int = 1;

static var multiplicator : int = 5;

function Update () { multiplicator = Time.time * 0.005 + 3;

 var theEnemy = GameObject.Find("Enemy(Clone)");

 if ( ! theEnemy  &&  Time.time > 5 ) { 
     levelNumber ++;
     amountEnemies += 5; 
     Invoke("Up", 0);
 }

 else { 
     CancelInvoke();
 }

}

function Up () { for ( i = 0; i < amountEnemies; i ++ ) {

     var enemyIndex = Random.Range(0, enemyPrefabs.length - 1);
     var spawnIndex = Random.Range(0, spawnPoints.length - 1);

     var obj : Transform  = enemyPrefabs[ enemyIndex ]; 
     var pos : Transform  = spawnPoints [ spawnIndex ];

     Instantiate(obj,pos.position,pos.rotation);
 } 

}

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 Duplicis · Dec 24, 2011 at 02:25 PM 0
Share

thank you very much

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

Making an Array of Spawnpoints... 1 Answer

How can i delete an item from an array after being used? (So, it won't be repeated) 2 Answers

Spawning a new player instance (UNET) 3 Answers

[SOLVED]How to use attributes from array of custom class instances? 1 Answer

Out of range error 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