IndexOutOfRangeException array index is out of range

Asked 1 years ago, Updated 1 years ago, 38 views

I am using Unity.I'm a beginner. When I run it, I get a console error.

IndexOutOfRangeException array index is out of range

and

The referenced script on this Behavior is missing

said he.

I looked into the error, but I didn't understand it well, so please let me know the solution.

using UnityEngine;
using System.Collections;

public class Player_NonPhysics 2D: MonoBehavior {
    // Declaration
    // Properties for Adjustment in Inspector
    public float speed = 15.0f;
    public Sprite [] run;
    public Sprite [ ] jump;

    // variables handled internally

    float jumpVy;
    intanimIndex;
    boolean goalCheck;

    // Code corresponding to the message
    // Start running components

    void Start() {
        // Initialization
        jumpVy = 0.0f;
        animIndex=0;
        goalCheck = false;
    }

    // Collisions of other game objects are included in the collision of player characters
    void OnCollisionEnter 2D (Collision 2D col) {
        // Goal check
        if(col.gameObject.name=="Stage_Gate"){
            // goal
            goalCheck = true;
            return;
        }
        // Reload and reset the stage if it's not a goal
        Application.LoadLevel(Application.loadedLevelName);
    }

    // Redrawing frames
    void Update() {
        if(goalCheck){//goalCheck)
            return;
            // If it's a goal, stop processing
        }

        // Calculate the height of the current player character
        float height = transform.position.y + jumpVy;
        // Ground check (grounded if height is 0)
        if(height<=0.0f){
            // Jump initialization
            height = 0.0f;
            jumpVy = 0.0f;
            // Jump Check
            if(Input.GetButtonDown("Fire1")}
                // Jump processing
                jumpVy=+1.3f;
                // Switch to jump sprite image...
                GetComponent<SpriteRender>().sprite=jump[0];
            } else{
                // Run processing
                animIndex++;
                if(animIndex<=run.Length){
                    animIndex=0;
                }
                // Switch to running sprite image
                GetComponent<SpriteRender>().sprite=run [animIndex];
            }
        } else{
            // During descent after jump
            jumpVy -= 0.2f;
            // jumpVy-=6.0f*Time.deltaTime;// Correct action here
        }

        // Move player character (coordinate setting)
        transform.position = new Vector3(
            transform.position.x+speed*Time.deltaTime, height, 0.0f);
        // It can be described as relative movement as shown below.
        // transform.Translate (speed*Time.deltaTime, jumpVy, 0.0f);
        // transform.position + = new Vector3 (speed*Time.deltaTime, jumpVy, 0.0f);
        // However, please note that the following writing method will not be used after rain.
        // transform.position.Set(
        // transform.position.x+speed*Time.deltime, height, 0, 0f);
        // Camera movement (relative movement of coordinates)
        GameObject goCam=GameObject.Find("Main Camera");
        goCam.transform.Translate(speed*Time.deltaTime, 0.0f, 0.0f);
    }
    // Displaying UnitGuI
    void OnGUI() {
        // Debug text
        GUI.TextField(newRect(10,10,300,60),
                  "[Unity2d Sample 3-1 A]\nPress the left mouse button to accelerate\nand jump";
        // Reset button
        if(GUI.Button(new Rect(10,80,100,20), "Reset"){
            Application.LoadLevel(Application.loadedLevelName);
        }
    }
}

c# unity3d

2022-09-29 21:26

2 Answers

IndexOutofRangeException array index is out of range

Unity Error List ページMiddle page

The referenced script on this Behavior is missing

[Unity] How to Find the Cause Object for Behaviour is missing!
will be helpful.

It may be difficult at first, but error investigation is a must for any development, so
Good luck (I think it's also good to learn how to view and use IDE).

----------

// Run Processing
animIndex++;
if(animIndex<=run.Length){
    animIndex=0;
}
// Switch to running sprite image
GetComponent<SpriteRender>().sprite=run [animIndex];  

I don't know the details because I can't check it on hand, but it's normal for animIndex to exceed the number of run elements.

This is the image of this implementation.

animeIndex=6;
// run.Length = 5
run = new[] {1,2,3,4,5}; 

// Six is greater than five, so it does not branch out.
if(6<=5){
    animIndex=0;
}
// "IndexOutofRangeException" because run has a maximum index of 4.
varsprite=run[6];


2022-09-29 21:26

Exception that occurs when array subscripts are less than 0 or more elements in array.


2022-09-29 21:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.