0

I am doing this button script so my sprite changes when I walk over it. However when I do so I get a IndexOutOfRange error. Could someone tell me what I' m doing wrong and maybe even correct me? Thank you for your help!

The code:

void OnTriggerEnter2D ()
     {
        int SpriteIndex = 1;
        if(SpriteIndex == 1)
          {
             SpriteIndex =- 1;
             Debug.Log(SpriteIndex);
          }
        else if(SpriteIndex == 0)
          {
             SpriteIndex =+ 1;
             Debug.Log(SpriteIndex);
          } 
        this.GetComponent<SpriteRenderer> ().sprite = hitSprites[SpriteIndex];
        Debug.Log(SpriteIndex);
     }
Hugo Woesthuis
  • 193
  • 2
  • 15
  • 1
    You are trying to set the sprite to the index of -1 in your first if-statement. You set the variable to 1, check if it's 1, and then set it to -1. The elseif will never be reached. – ryeMoss Dec 14 '17 at 16:44
  • I think you mean `SpriteIndex -= 1` instead of `SpriteIndex =- 1` (and similarly for `=+`), but that may be only one of several problems. – D Stanley Dec 14 '17 at 16:49
  • Thank you, It' s working now I only need to sort out why my colliders are coliding 2 times at once.... –  Dec 14 '17 at 17:14

0 Answers0