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

Animator Error

Discussion in 'Scripting' started by LPocay, Jun 26, 2015.

  1. LPocay

    LPocay

    Joined:
    Jun 21, 2015
    Posts:
    9
    Grettings! i have a problem with this code
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5.     public int speed;
    6.     public float jumpV;
    7.  
    8.     private bool isOnAir=false;
    9.     private bool walking;
    10.     Animator ani;
    11.  
    12.     void FixedUpdate(){
    13.         float MoveF=Input.GetAxis("Horizontal");
    14.         Moves (MoveF);
    15.         jump ();
    16.         Animating (MoveF);
    17.         Debug.Log (walking);
    18.     }
    19.            
    20.     void jump(){
    21.         if(Input.GetButton("Jump") && isOnAir==false){
    22.             GetComponent<Rigidbody>().AddForce(Vector3.up*jumpV);
    23.             isOnAir=true;
    24.             ani.SetBool("IsJumping",true);
    25.         }
    26.     }
    27.     void OnCollisionStay(Collision collisionInfo){
    28.         if (collisionInfo.gameObject.tag=="Walls") {
    29.  
    30.         } else {
    31.             isOnAir = false;
    32.         }
    33.     }
    34.     void Moves(float h){
    35.         Vector3 Move = new Vector3 (h, 0, 0);
    36.         GetComponent<Rigidbody> ().MovePosition (transform.position+Move*speed*Time.deltaTime);
    37.  
    38.     }
    39.     void Animating(float h){
    40.         if (h != 0f) {
    41.             walking = true;
    42.         } else {
    43.             walking=false;
    44.         }
    45.         ani.SetBool ("IsWalking", walking);
    46.     }
    47. }
    Im having this error: Assets/Scripts/PlayerController.cs(10,18): warning CS0649: Field `PlayerController.ani' is never assigned to, and will always have its default value `null'
    Help me please!
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    in c# if you omit "public" the variable is assumed to be "private". Private variables have to be assigned through scripts since they don't appear in the inspector.

    If you want to assign in the inspector you need to set it to public, if you want to assign it through code you need a

    Code (csharp):
    1.  
    2. ani = GetComponent<Animator>();
    3.  
    in the start function
     
    LPocay likes this.
  3. LPocay

    LPocay

    Joined:
    Jun 21, 2015
    Posts:
    9
    Thanks for help!