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 saberboy117 · Jul 12, 2011 at 11:52 PM · errorarrayindex

IndexOutOfRangeException: Array index is out of range

 IndexOutOfRangeException: Array index is out of range.
 Shield.Effect (RaycastHit ray) (at Assets/Scripts/Ships/Shield.js:17)
 UnityEngine.GameObject:SendMessage(String, Object)
 LaserRed:FixedUpdate() (at Assets/Scripts/Ships/Lasers/LaserRed.js:18)

I get that when my shield powers down and stops making effects, its designed to sort through different effects based on strength, however when it powers down and shrinks then it gives me this. Its weird

shield script

 var maxhealth : float;
 var healrate : float;
 var breaktime : float;
 var se  = new GameObject[4];
 private var health : float;
 private var healing : boolean = false;
 
 
 function Start(){health = maxhealth;}
 
 function Effect(ray : RaycastHit){
 var hp = (health/maxhealth*100);
 var rot = Quaternion.FromToRotation(Vector3.right, ray.normal);
 if (hp <= 100 && hp >= 70){Instantiate(se[1],ray.point,rot);}
 else if (hp <= 70 && hp >= 50){Instantiate(se[2],ray.point,rot);}
 else if (hp <= 50 && hp >= 25){Instantiate(se[3],ray.point,rot);}
 else{Instantiate(se[4],ray.point,rot);}
 }
 
 function Damage(d : float){
 health+=(-d);
 checkHealth();
 }
 
 function Healmanager(){
 healing = true;
 while (healing && healrate != 0){
 health+=healrate;
 if (health >= maxhealth){ health = maxhealth; healing = false;}
 else{
 yield WaitForSeconds(1);}
 }
 }
 
 
 function ShieldBreak(){
 var s : Vector3 = transform.localScale;
 transform.localScale = Vector3();
 healing = false;
 yield WaitForSeconds(breaktime);
 health = 1;
 transform.localScale = s;
 if (healing == false){Healmanager();}
 }
 
 function checkHealth(){
 if (health > maxhealth){health = maxhealth; return;}
 if (health <= 0){ShieldBreak();}
 else if (health < maxhealth && healing == false){Healmanager();}
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Graham-Dunnett · Jul 13, 2011 at 10:00 AM

The error you get is on line 17 in your script. When I count through the lines I see:

 else{Instantiate(se[4],ray.point,rot);}

This says that you are accessing the 5th element in the array, but you have declared the se array as having 4 elements. Arrays are zero indexed in Javascript, right?

Pro tip. The error messages should always tell you which line the problem occurred on. The error you posted says that you are indexing an array with a value that is wrong, and this occurs on line 17 of the script called Shield.js. From there it's trivial to find the problem.

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 saberboy117 · Jul 13, 2011 at 03:24 PM 0
Share

Oh, I see lol starts at zero thanks

avatar image
0
Wiki

Answer by awesomealex · May 25, 2012 at 05:01 PM

Hi guys !!!

I've got nearly the same error:

IndexOutOfRangeException: Array index is out of range. ConservePhysicScript.Start () (at Assets/Script/ConservePhysicScript.js:15)

here's my code (I'm a 3D artist and that's the first time I'm makinf .js so sorry if it's look likes horrible for ypu to read ...). The goal here is to instantiate on the position of an object to an other one.

Any idea will be welcome!!!

var container : GameObject;

var MeshTransform:GameObject[];

var IsConservePlieeBase=false;

var conservePliee: GameObject;

var conserveFermee: GameObject;

function Start () {

     container = GameObject.Find("conserve_container_prefab");
     conserveFermee = GameObject.Find("conserve_fermee_base");
     conservePliee = GameObject.Find("conserve_pliee_base");
     MeshTransform[0].renderer.enabled = true;
     MeshTransform[1].renderer.enabled = false;
     

}

function makeDamage(damage:Vector3){

 MeshTransform[0].renderer.enabled = false;
 MeshTransform[1].renderer.enabled = true;
 
 Instantiate (MeshTransform[0], transform.position,transform.rotation);

 conservePliee.rigidbody.AddForce(25*damage);

 print("+50 PTS");
 
 Destroy(conserveFermee);
 //print(MeshTransform[1].renderer.enabled);
 yield WaitForSeconds(5);
 Destroy(conservePliee);

}

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

C# array not behaving as expected 0 Answers

Array index is out of range Error, only when array is used to Instantiate 4 Answers

NullReferenceException array 2 Answers

array problem 1 Answer

Error CS0029 Help? (Screenshot of Exact 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