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

"Object reference not set to an instance of an Object" Error, I can't slide

Discussion in '2D' started by MazzasKarlanty, Sep 7, 2021.

  1. MazzasKarlanty

    MazzasKarlanty

    Joined:
    Jun 13, 2021
    Posts:
    4
    Hi, I've been working on my coding and I'm trying to slide my character, but when I make the condition my program says "Object reference not set to an instance of an Object".

    I searched the error and nothing. I'm new so if you can help me I appreciate your time.

    Here you have the code.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class crouch : MonoBehaviour
    6. {
    7.  
    8.     Rigidbody2D rb;
    9.  
    10.  
    11.     bool crouching;  //bool crouch
    12.  
    13.     bool iSliding = false; //bool slide
    14.     float vel_slide = .8f; //vel slide
    15.  
    16.     crouch thiScrip;
    17.  
    18.     public Transform groundcheck;
    19.     public float checkradius;
    20.     public LayerMask whatiscelling;
    21.  
    22.     public Collider2D cl;
    23.  
    24.     public corrermovimientogirar mov;
    25.  
    26.     void Awake(){
    27.  
    28.         thiScrip = GameObject.Find("player good good").GetComponent<crouch>();
    29.  
    30.     }
    31.  
    32.     void setup(){
    33.  
    34.         rb = GetComponent<Rigidbody2D>();
    35.  
    36.         cl = GetComponent<BoxCollider2D>();
    37.  
    38.         mov = GetComponent<corrermovimientogirar>();
    39.  
    40.        
    41.  
    42.     }
    43.  
    44.     void FixedUpdate()
    45.     {
    46.        
    47.         crouching = Physics2D.OverlapCircle(groundcheck.position, checkradius, whatiscelling);
    48.  
    49.        
    50.  
    51.         if (Input.GetKey(KeyCode.D) && (Input.GetKey(KeyCode.LeftShift))){
    52.  
    53.             if (Input.GetKey(KeyCode.S)){
    54.  
    55.                 preformslide_right();
    56.  
    57.             }
    58.  
    59.         }
    60.  
    61.         else if (Input.GetKey(KeyCode.S) || crouching){
    62.  
    63.             crouched();
    64.  
    65.         }
    66.  
    67.        
    68.  
    69.         else{
    70.  
    71.              cl.enabled = true;
    72.  
    73.             mov.vel_caminando = 0.4f;  // devolver valores de velocidad de srip movimientogrirar (valor caminando)
    74.             mov.vel_corriendo = .41f;  // devolver valores de velocidad de srip movimientogrirar (valor corriendo)
    75.  
    76.             crouching = false;
    77.  
    78.         }
    79.  
    80.        
    81.     }
    82.  
    83.     private void crouched(){
    84.  
    85.         cl.enabled = false;
    86.  
    87.         crouching = false;
    88.  
    89.         mov.vel_caminando = 0.22f;  // cambiando varibale velocidad corriendo scrip movimiento mientras esta agachado
    90.         mov.vel_corriendo = .205f;  // cmabiando varibale velocidad corriendo scrip movimiento mientras esta agachado
    91.  
    92.  
    93.     }
    94.  
    95.     private void preformslide_right(){
    96.  
    97.         rb.AddForce (new Vector3 (400 * vel_slide, 0, 0));
    98.  
    99.     }
    100. }
    101.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,965
    Really?! There's only one answer. One. There is no other way, and it doesn't even matter what you are doing.

    The answer is always the same... ALWAYS. It is the single most common error ever.

    Don't waste your life spinning around and round on this error. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception
    - also known as: Object reference not set to an instance of an object

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:

    - drag it in using the inspector
    - code inside this script initializes it
    - some OTHER external code initializes it
    - ? something else?

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.

    Here is a clean analogy of the actual underlying problem of a null reference exception:

    https://forum.unity.com/threads/nul...n-instance-of-an-object.1108865/#post-7137032
     
    MazzasKarlanty likes this.
  3. CONGOBILL

    CONGOBILL

    Joined:
    Mar 4, 2020
    Posts:
    17
    There can be many options, when you have an error, it would be good to look at what line you got the error (you can double click on it) to know which variable is giving you the problem. Also, try to avoid using GameObject.Find() as this is a common error for a Null variable.
     
  4. MazzasKarlanty

    MazzasKarlanty

    Joined:
    Jun 13, 2021
    Posts:
    4
    oh, thank you the error was that the rigidbody was null, I try putting a Deb.Log and it was saying that is nul. I added the rigidBogy.GetComponent to the void awake and now works perfecly.

    thank you.