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 nikhilVardhan · Jun 26, 2020 at 04:13 AM · scripting problemscripting beginner

Index Out Of Range Exception, Why i am getting this error?

Please help me. I am getting the following error four times, 'coz the loop is running for the four times: Index out of range exception: Index was outside of the bounds of the array.

This is my script:

public class enemySpawn : MonoBehaviour { public GameObject enemySpawnee; public GameObject playerObject;

 float InstantiationTimer = 2f;
 float xPos, yPos;

 Player playerScript;
 
 [HideInInspector]
 public int actualCount = 0;
 
 [HideInInspector]
 public bool[] spawnEnable= { true, true, true, true};
 
 private void Start()
 {
     playerScript = playerObject.GetComponent<Player>();
     Debug.Log(spawnEnable.Length);
     /*
     for (int i = 0; i < 4; i++)
     {
         spawnEnable[i] = true;
     }*/
 }

 void Update()
 {
     if(spawnEnable[0])
     {
     CreatePrefab1();
 }
     if (spawnEnable[1])
     {
         CreatePrefab2();
     }
     if (spawnEnable[2])
     {
         CreatePrefab3();
     }
     if (spawnEnable[3])
     {
         CreatePrefab4();
     }
 }
 private void OnTriggerExit(Collider collider)
 {
     if (collider.gameObject.tag == "Player")
     {
         if (playerScript.quaderentCheck == 1)
         {
             spawnEnable[0] = true;
         }
         if (playerScript.quaderentCheck == 2)
         {
             spawnEnable[1] = true;
         }
         if (playerScript.quaderentCheck == 3)
         {
             spawnEnable[2] = true;
         }
         if (playerScript.quaderentCheck == 4)
         {
             spawnEnable[3] = true;
         }
     }
 }
 void CreatePrefab1()
 {
     InstantiationTimer -= Time.deltaTime;
     
         if (InstantiationTimer <= 0)
         {

         if(actualCount < 5)
         {
             xPos = Random.Range(-65, -3);
             yPos = Random.Range(22, 3);

             Instantiate(enemySpawnee, new Vector3(xPos, yPos, 10), Quaternion.identity);    
         }
             InstantiationTimer = 2f;
             actualCount++;
     } }
 void CreatePrefab2()
 {
     InstantiationTimer -= Time.deltaTime;

     if (InstantiationTimer <= 0)
     {
         if (actualCount < 5)
         {
             xPos = Random.Range(6, 65);
             yPos = Random.Range(22, 3);

             Instantiate(enemySpawnee, new Vector3(xPos, yPos, 10), Quaternion.identity);
         }
         InstantiationTimer = 2f;
         actualCount++;
     }
 }
 void CreatePrefab3()
 {
     InstantiationTimer -= Time.deltaTime;

     if (InstantiationTimer <= 0)
     {

         if (actualCount < 5)
         {
             xPos = Random.Range(-65, -3);
             yPos = Random.Range(-4, -24);

             Instantiate(enemySpawnee, new Vector3(xPos, yPos, 10), Quaternion.identity);
         }
         InstantiationTimer = 2f;
         actualCount++;
     }
 }
 void CreatePrefab4() {
     InstantiationTimer -= Time.deltaTime;

     if (InstantiationTimer <= 0)
     {
         if (actualCount < 5) {
             xPos = Random.Range(6, 65);
             yPos = Random.Range(-4, -24);

             Instantiate(enemySpawnee, new Vector3(xPos, yPos, 10), Quaternion.identity); }
         InstantiationTimer = 2f;
         actualCount++;
     }}    }

@Hellium @Eric5h5 @clunk47 @Bunny83 @robertbu @aldonaletto @tanoshimi

Comment
Add comment · Show 6
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 ilusja · Jun 26, 2020 at 04:58 AM 0
Share

Hey, this code works in my project. By the way, you can also use public bool[] spawnEnable = {true, true, true, true};. Looks like maybe something didn't initialize in your case? Did you try debugging it? Are you sure the error is related to this line of code? spawnEnable[i] = true;

avatar image nikhilVardhan ilusja · Jun 26, 2020 at 05:06 AM 0
Share

Thank you, yes i tried to debug it, but it was fine. Yes error is co$$anonymous$$g out of every line where i am using spawnEnable[]. I tried the method you have mentioned above but i am still getting the same error. And i do not know what is wrong with my script. When i tried to check the length using: Debug.Log(spawnEnable.Length) not only it is giving me output - "0" but it is showing this four times even though i have put it in Start().

avatar image ilusja nikhilVardhan · Jun 26, 2020 at 05:10 AM 0
Share

How many gameObjects with this script attached do you have on your scene? And, could you show us whole script?

Show more comments
Show more comments

3 Replies

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

Answer by Siljias · Jun 26, 2020 at 05:28 AM

In your update loop, you are accessing the array with, "1,2,3,4". However, due to how array indexing works, you need to use "0,1,2,3".


spawnEnable[0] -> first element
spawnEnable[1] -> second element


NOT


spawnEnable[1] -> first element
spawnEnable[2] -> second element


I did not see anything in your start function that would throw that error. The update loop will definitely throw it tho.


EDIT


Your OnTriggerEnter() callback is also trying to access the array indices, starting with 1. That will probably also throw an error, once it gets to the last option.

Comment
Add comment · Show 15 · 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 nikhilVardhan · Jun 26, 2020 at 05:34 AM 0
Share

That was really silly of him, thank - you for pointing out. But error still persists sir.

avatar image ilusja nikhilVardhan · Jun 26, 2020 at 05:37 AM 0
Share

@nikhilVardhan Let us know in which line the error occurs. Did you enable "Clear on play" in Console?

avatar image Siljias nikhilVardhan · Jun 26, 2020 at 05:37 AM 0
Share

$$anonymous$$ake sure to change it in the OnTriggerExit() callback as well. And make sure all four options are changed, not just the first two that I wrote in the answer ;P

avatar image nikhilVardhan Siljias · Jun 26, 2020 at 05:40 AM 0
Share

Yes i have corrected at every single place, even in the another Player script which is accessing this variable also. It is showing error in line 39.

Yes, i always do clear my console. @ilusja @Siljias

Show more comments
avatar image
0

Answer by ilusja · Jun 26, 2020 at 05:32 AM

During the investigation of the code, I've discovered the thing:

you're using

  if (spawnEnable[4])
  {
       CreatePrefab4();
  }

in your Update() fuction. When you're getting an error, check in which line the error occurs (in my case, it says IndexOutOfRangeException: Index was outside the bounds of the array. EnemySpawn.Update () (at Assets/EnemySpawn.cs:46)). Also, you could read about how to debug your code, this will prevent this kind of problems in the future.


spawnEnable length is 4, this means you can access spawnEnable[0] (first element in the array) and spawnEnable[3] (last element), but not spawnEnable[4]. This is outside the bounds of the array.

As a side note, I would add [RequireComponent(typeof(Collider))] to your script and rename the class to EnemySpawn. You should avoid using small letters at the beginning of your classes names, usually PascalCase is used.


Please, let me know if it solved your problem :)

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 nikhilVardhan · Jun 26, 2020 at 05:37 AM 0
Share

No, in my case it started to pointing out from: if(spawnEnable[0]) // i corrected the code after @Siljias pointed out { createPrefab1(); }

avatar image ilusja nikhilVardhan · Jun 26, 2020 at 05:39 AM 0
Share

Ok, try to turn Unity off, and delete:

  • projectpath/*.sln,

  • projectpath/*.csproj,

  • projectpath/Temp,

  • projectpath/obj,

  • projectpath/Library/ScriptAssemblies,

turn the Unity on again and press play.

avatar image
0

Answer by Jon_Olive · Jun 27, 2020 at 07:49 PM

I have a feeling what might have happened is that you initially just declared the spawnEnable array without assigning a length or values and perhaps added those later. That would mean that it got serialised as an empty array of length zero - after which adding the hard coded values would have had no effect.

Do you actually need to serialise spawnEnable - or just have it be public? if the latter - replace [HideInInspector] with [NonSerialized] - it will still be public but wont show in the inspector and will not get serialised.

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 nikhilVardhan · Jun 28, 2020 at 04:51 AM 0
Share

I have solved this but it's still a not precise solution. What actually happened is, the variable was just a normal int variable initially, afterwards, i needed an array ins$$anonymous$$d so i changed the same variable to array. And that's why this whole thing has happened, the same thing is happened with an another variable when i changed it to an array. I don't know why this is happening, its like unity and studio are not in sync.

avatar image Jon_Olive nikhilVardhan · Jun 28, 2020 at 08:00 PM 0
Share

sounds like a Unity serialization bug then rather than an issue between unity and studio - it's getting confused when you keep the same variable name. I just tried it and indeed in both my scenario and yours you get the behaviour you experienced. Commenting out the variable declaration, letting unity recompile the script and then uncommenting sorts it out.

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

240 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GetComponent won't work 1 Answer

creating and using list of prefabs 2 Answers

Can I change animation key frame property in script? 1 Answer

How to Write a Drag and Drop script for Unity 5.3.3? 4 Answers

How can i upgrade the fire rate and damage in my upgrade menu 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