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

Bug Variable doesn't exist inside else statement

Discussion in 'Scripting' started by sachintha3232, May 15, 2020.

  1. sachintha3232

    sachintha3232

    Joined:
    Aug 14, 2019
    Posts:
    2
    I am trying to get a 2d sprite to play a left / right facing idle animation based on the way he's facing. I have a number called isLeft which changes accordingly. My issue is at the bottom of my code I have an else statement where it says isLeft does not exist in the current context. It works for all the other if statements which is why I'm confused.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class basicMovement : MonoBehaviour
    4. {
    5.     public float MovementSpeed = 1;
    6.     public float JumpForce = 3;
    7.     public float isLeft = 1;
    8.  
    9.     public Animator animator;
    10.     private Rigidbody2D rb2d;
    11.  
    12.     private void Start(){
    13.         animator = GetComponent<Animator>();
    14.         rb2d = GetComponent<Rigidbody2D>();
    15.     }
    16.  
    17.     private void Update(){
    18.  
    19.         if(Input.GetKey("right")){
    20.             rb2d.velocity = new Vector2(MovementSpeed, rb2d.velocity.y);
    21.             animator.Play("rightRun");
    22.             isLeft = 0;
    23.         }
    24.         else if(Input.GetKey("left")){
    25.             rb2d.velocity = new Vector2(-MovementSpeed, rb2d.velocity.y);
    26.             animator.Play("leftRun");
    27.             isLeft = 1;
    28.         }
    29.         else{
    30.             if(isleft == 1){
    31.                 animator.Play("leftIdle");
    32.             }
    33.             else{
    34.                 animator.Play("rightIdle");
    35.             }
    36.         }
    37.     }
    38. }
    39.  
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,204
    Captialization is important!

    The variable's named "isLeft", not "isleft"
     
    sachintha3232 likes this.
  3. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,723
    UpperCase buddy o_<

    isleft > isLeft
     
    sachintha3232 likes this.
  4. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,078
    If you are using Visual Studio, you can put your caret inside
    isleft
    and press [Ctrl]+[Space] this will convert it to
    isLeft
    (or shows you some suggestions what you might want to write).
     
  5. sachintha3232

    sachintha3232

    Joined:
    Aug 14, 2019
    Posts:
    2
    Thanks so much everyone who helped out, I'm an idiot I didn't see that lmao. I would've gotten ripped to pieces on stackoverflow so this community is already a pleasant surprise :D:D:D