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 Ellis · Mar 03, 2013 at 10:57 AM · errorarraysrangeindex

Array index is out of range?

I have searched around a bit and found tons of awnsers but nothing that really helped me!

Here is my code:

 var Health : int = 50;
 var prefabBullet:Transform;
 var shootForce:float;
 var jump : GameObject;
 var smoke : GameObject;
 var smoking : int = 20;
 var HealthPoint : Texture[];
 var SpawnPoints : Transform[];
 var Deaths : AudioClip[];
 var Jump : AudioClip[];
 
 
 function Update () {
     if(Health <= 0){
         var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity);
         instanceBullet.rigidbody.AddForce(transform.forward * shootForce);
         death();
         Health = 50;
         
     }
     
     if(Health <= smoking)
         smoke.active = true;
         
     if(Health >= smoking)
         smoke.active = false;
     
     if (Input.GetButtonDown("Jump")){
         audio.clip = Jump[0];
         audio.Play();
         jump.active = true;
     }
     if (Input.GetButtonUp("Jump")){
         audio.clip = Jump[0];
         audio.Stop();
         jump.active = false;
     }
 }
 
 
 function OnCollisionEnter(collision : Collision){
     if(collision.gameObject.tag == "Bullet"){
         var randDamage = Random.Range(1, 5);
         var damage = randDamage;
         
         switch(damage){
             case 1:
                 Health -=1;
             break;
             case 2:
                 Health -=2;
             break;
             case 3:
                 Health -=3;
             break;
             case 4:
                 Health -=4;
             break;
             case 5:
                 Health -=5;
             break;
         }
     }
 }
 
 
 function OnTriggerEnter(other : Collider){
     if(other.tag == "Health"){
         Health += 1*Time.deltaTime;
         
         if(Health >= 50){
             Health = 50;
         }
     }
 }
 
 
 function OnGUI() {
     if(Health <= 50){
         GUI.DrawTexture(Rect(0.0f,0.0f,Screen.width,Screen.height), HealthPoint[5]);
     }
     if(Health <= 40){
         GUI.DrawTexture(Rect(0.0f,0.0f,Screen.width,Screen.height), HealthPoint[4]);
     }
     if(Health <= 30){
         GUI.DrawTexture(Rect(0.0f,0.0f,Screen.width,Screen.height), HealthPoint[3]);
     }
     if(Health <= 20){
         GUI.DrawTexture(Rect(0.0f,0.0f,Screen.width,Screen.height), HealthPoint[2]);
     }
     if(Health <= 10){
         GUI.DrawTexture(Rect(0.0f,0.0f,Screen.width,Screen.height), HealthPoint[1]);
     }
     if(Health <= 1){
         GUI.DrawTexture(Rect(0.0f,0.0f,Screen.width,Screen.height), HealthPoint[0]);
     }
 }
 
 
 function death(){
     AudioSource.PlayClipAtPoint(Deaths[ Random.Range(0, Deaths.Length)], transform.position, 2.0f);
      i = Random.Range(0, SpawnPoints.Length);
      transform.position = SpawnPoints[i].position;
     transform.rotation = SpawnPoints[i].rotation;
 }

Error message: IndexOutOfRangeException: Array index is out of range. Player.OnGUI () (at Assets/Scripts/Player.js:80)

Comment
Add comment · Show 4
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 robertbu · Mar 03, 2013 at 11:02 AM 0
Share

The most likely culprit is the HealthPoint[] array. It is expecting to have 6 textures, and most likely these textures need to be assigned in the inspector:

  • In the Hierarchy, select the game object this script is attached to.

  • Find the HelpPoint[] array in the inspector

  • Click on the triangle to the left of the name

There should be six initialized entries in this list. If there are not 6 or if any of them are empty, you need to drag new textures onto the empty slots.

If this is not the error, then I suggest you double click on the error in Unity and then view the line the error is on in $$anonymous$$ono. Then edit your question placing a comment on the line that has the error. The line numbers displayed above and the line number in Unity don't match, so it can be difficult to figure out the location of an error.

avatar image Ellis · Mar 03, 2013 at 11:07 AM 0
Share

There is 6 textures, I can put what ever number I want in the inspector and it will give me this error! It's where I put the [5], I can put in 1-1000 there to and it says the same thing!

avatar image Ellis · Mar 03, 2013 at 11:12 AM 0
Share

it draws the texture but lag out the game cuz of the error!

avatar image robertbu · Mar 03, 2013 at 03:43 PM 0
Share

It not just the number, but all six slots have to be filled with textures that you dragged and dropped into the six slots. If all six slots are filled, then the next step is to place a comment in the code above so we can see which line is causing the error.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by skyassasin22 · Mar 03, 2013 at 11:06 AM

Go to the gameobject where you attached this script, look in the inspector and look for the Health Point drop down and set the size the 6. I hope it helps.

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 Ellis · Mar 03, 2013 at 11:09 AM 0
Share

nope, I can put in 10 there and it still gives me the error! I can put in 0-5 in the [5] as well and it still gives me that error!

avatar image Ellis · Mar 03, 2013 at 11:19 AM 0
Share

I think it's really weird since it shows the right texture and all! It actually find the texture I have put in the slot that I want it to look for but still it says that it's out of range!

avatar image
0

Answer by Ellis · Mar 03, 2013 at 03:49 PM

O gawd, never mid! I forgot that I had put this script on another object as well! xD

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

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

10 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

Related Questions

Multiple Cars not working 1 Answer

Error when trying to set variable with array 1 Answer

BCE0044 expected EOF and } found 'else' Help me Please!! 1 Answer

Runtime Error 1 Answer

Internal compiler error. See the console log for more information. output was: 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