Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Error: Array index (0) is out of bounds (size=0)

Discussion in 'Scripting' started by yourapps26, Aug 6, 2020.

  1. yourapps26

    yourapps26

    Joined:
    Jul 28, 2020
    Posts:
    6
    I set it to find the blend Shape Count but it is set to 0 then printed so then the error shows.

    Code (CSharp):
    1.    
    2.     private void Start()
    3.     {
    4.         rend = GetComponent<SkinnedMeshRenderer>();
    5.         cubeSpring = GetComponent<SkinnedMeshRenderer>().sharedMesh;
    6.         blendShapeCount = cubeSpring.blendShapeCount;
    7.         print("count " + blendShapeCount);
    8.      
    9.     }
    10.  
    11.      private void Update()
    12.     {
    13.      
    14.         rend.SetBlendShapeWeight(0, weight);
    15.         realWeight = Mathf.Lerp(realWeight, weight, 30f * Time.deltaTime);
    16.     }
    17.  
    18.  
    19.     public void activateSpring()
    20.     {
    21.         weight = 100f;
    22.         StartCoroutine(wait(0.25f));
    23.     }
    24.  
    25.  
    26.     private IEnumerator wait(float sec)
    27.     {
    28.         yield return new WaitForSeconds(sec);
    29.         this.weight = 0f;
    30.         goingUp = false;
    31.         yield break;
    32.     }
    33.  
     
    Last edited: Aug 6, 2020
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Please give the exact line where the error occurs, and spell out what your actual question is.

    Index 0 is outside the bounds of a 0-length array, if that's what you're confused about. (Arrays are numbered from 0 to n-1; for example, an array of size 10 has elements numbered {0, 1, 2, ..., 8, 9}. "10" would be out-of-bounds of a 10-size array. Similarly, "0" is out-of-bounds of a 0-size array.)
     
  3. yourapps26

    yourapps26

    Joined:
    Jul 28, 2020
    Posts:
    6
    this is the line.
    rend.SetBlendShapeWeight(0, weight);
     
  4. yourapps26

    yourapps26

    Joined:
    Jul 28, 2020
    Posts:
    6
    Is there a way to change the out of bounds number?
     
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Make sure your cubeSpring has at least one blend shape.

    (you are trying to change the weight of whichever blend shape is first in the list, but there is no "first" because the list is completely empty)
     
  6. yourapps26

    yourapps26

    Joined:
    Jul 28, 2020
    Posts:
    6
    Can you make blend shapes in blander?
     
  7. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    I don't actually know what a "blend shape" is. I just know that you're trying to modify one that you don't have.

    Presumably there was some reason that you wrote the above code in the first place that tries to modify a blend shape. If you trace that reasoning backwards, you should eventually run into some bit of logic that rested on the belief that you would have one to modify. Either that was an unreasonable belief, in which case you need to change it and restart your chain of reasoning from that point, or it was a reasonable belief, in which case it should reference a reason why you were going to have a blend shape, and you should chase that down to figure out why you don't have one now.
     
    Bunny83 likes this.
  8. yourapps26

    yourapps26

    Joined:
    Jul 28, 2020
    Posts:
    6
    Oh I got it. In blender I made key shapes and unity was able to uses them.