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. Dismiss Notice

NullReferenceException: object reference not set to an instance of an object

Discussion in 'Scripting' started by CrossM, Mar 6, 2015.

  1. CrossM

    CrossM

    Joined:
    Mar 5, 2015
    Posts:
    4
    I keep getting this error, but can't find how to fix it anywhere on the internet.( the comments are in portuguese, so ignore :) )

    NullReferenceException: object reference not set to an instance of an object

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class quadMoveG : MonoBehaviour {
    5.    
    6.     public float maxspeed = 10f;//velocidade maxima
    7.     bool facingR = true; // indica se esta virado para direita ou n
    8.    
    9.     Animator anim; // variavel usada para indicar o animator
    10.    
    11.     //public bool jump = false; // indica se esta pulando ou nao
    12.     //public float jumpForce = 700f; // força do pulo
    13.    
    14.     public Transform groundCheck; //checa se tem chao ou nao
    15.     bool grounded = false; // mostra se esta no chao ou nao
    16.     float groundRadius = 0.2f; // quao grande e a hitbox
    17.     public LayerMask whatIsGround; //diferenciar demais objetos do chao
    18.    
    19.    
    20.     // Use this for initialization
    21.     void Start () {
    22.        
    23.     }
    24.    
    25.     // FixedUpdate age o tempo todo
    26.     void FixedUpdate () {
    27.         grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
    28.         // fala que esta no chao sempre que groundradius colidir com algum collider
    29.        ----------error--------> anim.SetBool ("Ground", grounded);  <----------error-------------// necessario somente quando tiver animaçoes, pois mostra para elas se esta no chao ou n
    30.            
    31.        
    32.         float move = Input.GetAxis ("Horizontal");// pega o comando para virar
    33.         //indica com qual velocidade o corpo ira se mover de acordo com a direçao dada acima
    34.         GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxspeed, GetComponent<Rigidbody2D>().velocity.y);
    35.         //flip
    36.         if (move > 0 && !facingR)// direita e considerada > 0  pelo unity
    37.             Flip ();
    38.         else if (move < 0 && facingR)// esquerda e considerada < 0 pelo unity
    39.             Flip ();
    40.     }
    41.     void Flip()// usado para virar o persongem (inverte a animaçao
    42.     {
    43.         facingR = !facingR; // para inverter para onde esta olhando
    44.         Vector2 theScale = transform.localScale; // armazena o valor de localScale
    45.         theScale.x *= -1; // Inverte o valor de theScale
    46.         transform.localScale = theScale; // Coloca o valor de volta para localScale
    47.     }
    48.    
    49. }
    50.  
    thanks :D
     
  2. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    There is no animator set in the script. If you would use 'Debug.Log(anim)' you will see it logs 'null'.
     
    CrossM likes this.
  3. CrossM

    CrossM

    Joined:
    Mar 5, 2015
    Posts:
    4
    Thank you very much!!

    I've forgotten to add - anim = GetComponent<Animator> (); on Start()

    Since I started it yesterday a lot os silly mistakes still happen, but thanks.