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 kivakabuto, Oct 13, 2014.

Thread Status:
Not open for further replies.
  1. nazarine

    nazarine

    Joined:
    Apr 9, 2017
    Posts:
    1
    I got similar problems and it was due to a few slight mistakes in the the tutorial videos (or maybe i missed steps)
    Mistake 1: The Zombunny was not assigned the Layer "Shootable" - so shots just bounced off it.
    Mistake 2: The Prefab HitParticles component was not copied to the zombunny child (Paste as new component bit same as for gun) - this caused the Null exception error in the enemyhealth script. I don't remember seeing the step in the tutorials that mentioned adding this particle to the zombunny.
     
  2. heshamMm

    heshamMm

    Joined:
    Jun 26, 2017
    Posts:
    1
    I have the same problem --- what Fix it for me either restart the unity editor or
    - i had both the child and the parent (including player , gun and gunbarrel) taged as player so when i untag the player character (the child) it works .
    i hope that help anyone face the same problem after me :D
     
  3. YT5h4dow

    YT5h4dow

    Joined:
    Feb 18, 2017
    Posts:
    6
    im trying to make it where when i walk into a box collider a bunch of zombies spawn at the spawn points but i just get this error any help

    NullReferenceException: Object reference not set to an instance of an object
    zombieSpawn.OnTriggerEnter (UnityEngine.Collider other) (at Assets/scripts/zombieSpawn.js:17)

    Code (JavaScript):
    1.  
    2. var Zombie : Transform;
    3. var ZombieSpawn1 : Transform;
    4. var ZombieSpawn2 : Transform;
    5. var ZombieSpawn3 : Transform;
    6. var ZombieSpawn4 : Transform;
    7. var ZombieSpawn5 : Transform;
    8.  
    9.  
    10. public var ZombiesSpawned = 0;
    11.  
    12. function OnTriggerEnter (other : Collider) {
    13.     if (other.tag == "Player")
    14.     {
    15.         if (ZombiesSpawned < 5)
    16.             {
    17.         Instantiate(Zombie, ZombieSpawn1.position, Quaternion.Identity);
    18.         ZombiesSpawned += 1;
    19.  
    20.         }
    21.     }
    22. }
     
  4. amstaker

    amstaker

    Joined:
    Sep 30, 2017
    Posts:
    2
    After testing this thing a ton, I believe it's a simple configuration issue. There is a checkbox in the Zombear and Hellephant Animator, "Apply Root Motion" checking this box and validating that the correct Enemy Attack, Enemy Health, and Enemy Movement scripts are called resolved this issue for me. Took forever to find.
     
  5. mykzgonzalez

    mykzgonzalez

    Joined:
    Feb 20, 2018
    Posts:
    8
    I'm having the same error msg..

    NullReferenceException: Object reference not set to an instance of an object
    StoriesDialogManagers.StartDialogue (.StoriesDialog dialogue) (at Assets/Scripts/Dialogue/StoriesDialogManagers.cs:23)
    StoriesDialogTrigger.Start () (at Assets/Scripts/Dialogue/StoriesDialogTrigger.cs:16)

    here is my code in c#..

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.SceneManagement;
    6. using UnityEngine.UI;
    7. public class StoriesDialogManagers : MonoBehaviour
    8. {
    9.     public Text nameText;
    10.     public Text dialogueText;
    11.     public string sceneName = "";
    12.     private Queue<string> sentences;
    13.     private void Start()
    14.     {
    15.         sentences = new Queue<string>();
    16.     }
    17.     public void StartDialogue(StoriesDialog dialogue)
    18.     {
    19.         nameText.text = dialogue.name;
    20.         sentences.Clear();
    21.         foreach (string sentece in dialogue.sentences)
    22.         {
    23.             sentences.Enqueue(sentece);
    24.         }
    25.         DisplayNextSentece();
    26.     }
    27.     public void DisplayNextSentece()
    28.     {
    29.         if (sentences.Count == 0)
    30.         {
    31.             EndDialogue();
    32.             return;
    33.         }
    34.         string sentence = sentences.Dequeue();
    35.         StopAllCoroutines();
    36.         StartCoroutine(TypeSentence(sentence));
    37.     }
    38.     IEnumerator TypeSentence(string sentence)
    39.     {
    40.         dialogueText.text = "";
    41.         foreach (char letter in sentence.ToCharArray())
    42.         {
    43.             dialogueText.text += letter;
    44.             yield return null;
    45.         }
    46.     }
    47.     public void EndDialogue()
    48.     {
    49.         SceneManager.LoadScene(sceneName);
    50.     }
    51. }
    52.  
    53.  
    In Unity..

    upload_2018-3-21_20-40-2.png

    This is bit weird because this is the only scene that my code isn't working..
     
    Last edited: Mar 22, 2018
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    dont use "icode" tags, use "code" tags, it'll include the line numbers so we can see which line the error is being generated on.

    Null reference errors only occur when you attempt to access a method or property of something that doesn't exist (i.e. its null and you're attempting to access it :cool:).

    Go to line 23, figure out which object is null, figure out why it's null.


    Side note, having two different spellings of "sentence" isn't looking too great :p
     
  7. N00MKRAD

    N00MKRAD

    Joined:
    Dec 31, 2013
    Posts:
    210
    Nice necro lol.

    Anyway, make sure you don't have a 2nd script in your scene, just search "t:StoriesDialogManager" in the hierarchy.
     
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
  9. mykzgonzalez

    mykzgonzalez

    Joined:
    Feb 20, 2018
    Posts:
    8
    I don't know what happen but what fixed the problem was I have created another EmptyGO and put the manager script and removed it from the other GO. Still weird because on my other scene, it works fine when the StoriesDialogManager and StoriesDialogTrigger are on the same GO. well hope this will help soon. :) thanks guys.
     
  10. nikhilprostudio

    nikhilprostudio

    Joined:
    Aug 24, 2018
    Posts:
    1
  11. Millimedia_Games

    Millimedia_Games

    Joined:
    Dec 31, 2017
    Posts:
    1
    I was having a different problembut found that very helpful. Thanks!
     
  12. Emperor26

    Emperor26

    Joined:
    Jun 24, 2018
    Posts:
    1
    Thank you Sir..
     
  13. binaryiris

    binaryiris

    Joined:
    Nov 16, 2018
    Posts:
    1
    Hey Mike!

    Sorry to bother with this but couldn't find an asnwer from the Google. Referring to the Unity 3d survival shooter lesson 9, where I should add PlayerHealthScript to the EnemyManager. The problem is when I try to drag&drop the Player-object from the hierarchy-view, i cannot add it to the corresponding place in the EnemyManager-Inspector-view? Plz Hlp! --Sigma
     
  14. irvannevki

    irvannevki

    Joined:
    Aug 24, 2019
    Posts:
    1
    guys..i wanna ask something... iam still newbie in AR development so i took a tutorial pack on asset store name nightmare . iam still learn from here. so the question is when i want to test the AR there is a notice about
    NullReferenceException: Object reference not set to an instance of an object
    InputHandler.Update () (at Assets/myassests/Objek/Script/InputHandler.cs:10)
    THE SCRIPT
    using UnityEngine;
    using System.Collections;

    public class InputHandler : MonoBehaviour
    {
    void Update ()
    {
    if(Input.GetMouseButton(0))
    {
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit rayCastHit;

    if(Physics.Raycast(ray.origin, ray.direction, out rayCastHit, Mathf.Infinity))
    {
    Door door = rayCastHit.transform.GetComponent<Door>();
    if(door)
    {
    door.PlayDoorAnim();
    }
    }
    }
    }

    void OnGUI()
    {
    GUI.Label(new Rect(10, 10, 500, 20), "Click on the door to trigger the animations!");
    }
    }
     
  15. rafasamir

    rafasamir

    Joined:
    Feb 29, 2020
    Posts:
    1
    Hello,

    I have this error, and i have no idea from where it comes?
    Can somone help me
    NullReferenceException: Object reference not set to an instance of an object
    GameController.Movement () (at Assets/Script/GameController.cs:60)
    GameController.TimerInvoke () (at Assets/Script/GameController.cs:44)


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;

    public class GameController : MonoBehaviour
    {
    public int maxSize;
    public int currentSize;
    public int xBound;
    public int yBound;
    public int score;
    public GameObject foodPrefab;
    public GameObject currentFood;
    public GameObject snakePrefab;
    public SnakeController head;
    public SnakeController tail;
    public int NESW;

    public Vector2 nexPos;
    // Start is called before the first frame update

    void OnEnable()
    {
    SnakeController.hit += hit;
    }
    void Start()
    {
    InvokeRepeating("TimerInvoke", 0, .5f);
    FoodFunction();
    }
    void OnDisable()
    {
    SnakeController.hit -= hit;
    }

    // Update is called once per frame
    void Update()
    {
    ComChangeD();
    }
    void TimerInvoke()
    {
    Movement();
    if (currentSize >= maxSize)
    {
    TailFunction();
    }
    else
    {
    currentSize++;
    }
    }



    void Movement ()
    {
    GameObject temp;
    nexPos =head.transform.position;
    switch (NESW)
    {
    case 0:
    nexPos = new Vector2(nexPos.x, nexPos.y + 1);
    break;
    case 1:
    nexPos = new Vector2(nexPos.x+1, nexPos.y);
    break;
    case 2:
    nexPos = new Vector2(nexPos.x, nexPos.y - 1);
    break;
    case 3:
    nexPos = new Vector2(nexPos.x-1, nexPos.y);
    break;
    }

    temp = (GameObject)Instantiate(snakePrefab, nexPos,transform.rotation);
    head.Setnext(temp.GetComponent<SnakeController>());
    head = temp.GetComponent<SnakeController>();


    return;

    }
    void ComChangeD()
    {
    if(NESW!=2&& Input.GetKeyDown(KeyCode.W))
    {
    NESW = 0;
    }
    if (NESW != 3 && Input.GetKeyDown(KeyCode.D))
    {
    NESW = 1;
    }
    if (NESW != 0 && Input.GetKeyDown(KeyCode.S))
    {
    NESW = 2;
    }
    if (NESW != 1 && Input.GetKeyDown(KeyCode.A))
    {
    NESW = 3;
    }
    }

    void TailFunction()
    {
    SnakeController tempSnake = tail;
    tail = tail.GetNext();
    tempSnake.RemoveTail();
    }

    void FoodFunction()
    {
    int xPos = Random.Range(-xBound, xBound);
    int yPos = Random.Range(-yBound, yBound);

    currentFood = (GameObject)Instantiate(foodPrefab, new Vector2(xPos, yPos), transform.rotation);
    StartCoroutine(CheckRender(currentFood));
    }

    IEnumerator CheckRender(GameObject IN)
    {
    yield return new WaitForEndOfFrame();
    if (IN.GetComponent<Renderer>().isVisible == false)
    {
    if (IN.tag == "Food")
    {
    Destroy(IN);
    FoodFunction();
    }



    }
    }

    void hit(string WhatWasSent)
    {
    if (WhatWasSent == "Food")
    {
    FoodFunction();
    maxSize++;
    score++;
    }
    if (WhatWasSent == "Snake")
    {
    CancelInvoke("TimerInvoke");
    Exit();
    }

    }
    void Exit()
    {
    SceneManager.LoadScene(0);
    }
    }
     
  16. ayush540

    ayush540

    Joined:
    Apr 2, 2020
    Posts:
    1
    NullReferenceException: Object reference not set to an instance of an object
    PaddleController.TouchMove () (at Assets/Scripts/PaddleController.cs:39)
    I dont know how to resolve it


    my PaddleController file:


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PaddleController : MonoBehaviour
    {
    // Start is called before the first frame update

    Rigidbody2D rb;

    public float moveSpeed;
    private void Awake()
    {
    rb = GetComponent<Rigidbody2D>();

    }

    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    private void FixedUpdate()
    {
    TouchMove();
    }

    void TouchMove()
    {
    Debug.Log(Input.GetMouseButton(0));
    if (Input.GetMouseButton(0))
    {
    Vector2 touchPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    if (touchPos.x < 0)
    {
    //move left
    rb.velocity = Vector2.left * moveSpeed;


    }
    else
    {
    //move right
    rb.velocity = Vector2.right * moveSpeed;

    }
    }
    else
    {
    rb.velocity = Vector2.zero;
    }


    }
    }


    Ball file:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Ball : MonoBehaviour
    {
    Rigidbody2D rb;
    public float bounceForce;

    // Start is called before the first frame update

    private void Awake()
    {
    rb = GetComponent<Rigidbody2D>();
    }

    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
    if (Input.anyKeyDown)
    {
    startBounce();
    }
    }

    void startBounce()
    {
    Vector2 randomDirection = new Vector2(Random.Range(-1, 1), 1);

    rb.AddForce(randomDirection * bounceForce, ForceMode2D.Impulse);


    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
    if (collision.gameObject.tag == "FallCheck")
    {
    GameManager.instance.restartGame();
    }
    }
    }


    GameManager file:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;

    public class GameManager : MonoBehaviour
    {

    public static GameManager instance;

    private void Awake()
    {
    instance = this;
    }

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    public void restartGame()
    {
    SceneManager.LoadScene("BounceBall");
    }
    }
     
  17. Benjohnhuidrom

    Benjohnhuidrom

    Joined:
    Sep 10, 2020
    Posts:
    1
    NullReferenceException: Object reference not set to an instance of an object
    GoogleARCore.Examples.Common.DetectedPlaneGenerator.Update () (at Assets/GoogleARCore/Examples/Common/Scripts/DetectedPlaneGenerator.cs:64)

    I also have the problem. But unable to rectify the problem. Please help.
    I am trying to create a AR app using ground plane detection.
     
  18. phillipglacanlale

    phillipglacanlale

    Joined:
    Sep 11, 2020
    Posts:
    1
    I'm having trouble and the error says NullReferenceException: Object reference not set to an instance of an object
    EnemyAttack.Update () (at Assets/Scripts/Enemy/EnemyAttack.cs:49)


    My Code:

    using UnityEngine;
    using System.Collections;

    public class EnemyAttack : MonoBehaviour
    {
    public float timeBetweenAttacks = 0.5f;
    public int attackDamage = 10;


    Animator anim;
    GameObject player;
    PlayerHealth playerHealth;
    EnemyHealth enemyHealth;
    bool playerInRange;
    float timer;


    void Awake ()
    {
    player = GameObject.FindGameObjectWithTag ("Player");
    playerHealth = player.GetComponent <PlayerHealth> ();
    enemyHealth = GetComponent<EnemyHealth>();
    anim = GetComponent <Animator> ();
    }


    void OnTriggerEnter (Collider other)
    {
    if(other.gameObject == player)
    {
    playerInRange = true;
    }
    }


    void OnTriggerExit (Collider other)
    {
    if(other.gameObject == player)
    {
    playerInRange = false;
    }
    }


    void Update ()
    {
    timer += Time.deltaTime;

    if(timer >= timeBetweenAttacks && playerInRange && enemyHealth.currentHealth > 0)
    {
    Attack ();
    }

    if(playerHealth.currentHealth <= 0)
    {
    anim.SetTrigger ("PlayerDead");
    }
    }


    void Attack ()
    {
    timer = 0f;

    if(playerHealth.currentHealth > 0)
    {
    playerHealth.TakeDamage (attackDamage);
    }
    }
    }
     
  19. Zetlas

    Zetlas

    Joined:
    Mar 18, 2021
    Posts:
    1
    using UnityEngine;

    public class PlayerMovement : MonoBehaviour
    {
    // This is a referance to the Rigidbody component called "rb"
    public Rigidbody rb;

    public float forwardForce = 2000f;
    public float sidewaysForce = 500f;

    // We marked this as "FixedUpdate" we are using it to mess with physics
    void FixedUpdate()
    {
    rb.AddForce(0, 0, forwardForce * Time.deltaTime); // Add a forward force

    if(Input.GetKey("d"))
    {
    rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    }

    if(Input.GetKey("a"))
    {
    rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    }

    if(rb.position.y < -3f)
    {
    FindObjectOfType<GameManager>().EndGame();
    }
    }
    }

    mine says the
    NullReferenceException: Object reference not set to an instance of an object
    PlayerMovement.FixedUpdate () (at Assets/Scripts/PlayerMovement.cs:28)
    I think the error is where i marked but i cant figure out what i did wrong
     
  20. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,498
    Please don't post the same question again and again to seven-year-old threads. It is actually explicitly against forum rules.

    For null reference, the answer is always the same... ALWAYS. It's like war... war never changes. 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.

    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.
     
  21. NotAidenYT

    NotAidenYT

    Joined:
    May 12, 2021
    Posts:
    1
    Im getting the same problolem how do i fix
     
  22. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,498
    Which of these three steps are you unable to perform?

    - Identify what is null
    - Identify why it is null
    - Fix that.

    Read the post directly above your post.
     
  23. soumya_alphpi

    soumya_alphpi

    Joined:
    Mar 3, 2021
    Posts:
    6
    Hello, I am stuck with this error can anybody help me?
    NullReferenceException: Object reference not set to an instance of an object
    GvrTrackedController.OnEnable () (at Assets/GoogleVR/Scripts/Controller/GvrTrackedController.cs:166)
    I am trying to make a VR classroom after adding GvrControllerPoiner the above error shows, please help me out.
     
    Last edited: May 30, 2021
  24. soumya_alphpi

    soumya_alphpi

    Joined:
    Mar 3, 2021
    Posts:
    6
    I had an error in Unity console
    NullReferenceException: Object reference not set to an instance of an object
    GvrTrackedController.OnEnable () (at Assets/GoogleVR/Scripts/Controller/GvrTrackedController.cs:167)
    please somebody help me out.
     
  25. Galileuflavio

    Galileuflavio

    Joined:
    Jan 10, 2018
    Posts:
    2
    using UnityEngine;
    using System.Collections;

    public class PlayerHealth : MonoBehaviour
    {
    public float health = 100f; // A saúde do jogador.
    public float repeatDamagePeriod = 2f; // Com que frequência o jogador pode ser danificado.
    public AudioClip[] ouchClips; // Matriz de clipes para reproduzir quando o player está danificado.
    public float hurtForce = 10f; // A força com que o jogador é empurrado quando machucado.
    public float damageAmount = 10f; // A quantidade de dano a receber quando os inimigos tocam o jogador

    private SpriteRenderer healthBar; // Referência ao renderizador de sprite da barra de saúde.
    private float lastHitTime; // A hora em que o jogador foi atingido pela última vez.
    private Vector3 healthScale; // A escala local da barra de saúde inicialmente (com saúde total).
    private PlayerControl playerControl; // Referência ao script de controle do jogador.
    private Animator anim; // Referência para o Animator no player


    void Awake ()
    {
    // Setting up references.
    playerControl = GetComponent<PlayerControl>();
    healthBar = GameObject.Find("HealthBar").GetComponent<SpriteRenderer>();
    anim = GetComponent<Animator>();

    // Getting the intial scale of the healthbar (whilst the player has full health).
    healthScale = healthBar.transform.localScale;
    }


    void OnCollisionEnter2D (Collision2D col)
    {
    // If the colliding gameobject is an Enemy...
    if(col.gameObject.tag == "Enemy")
    {
    // ... and if the time exceeds the time of the last hit plus the time between hits...
    if (Time.time > lastHitTime + repeatDamagePeriod)
    {
    // ... and if the player still has health...
    if(health > 0f)
    {
    // ... take damage and reset the lastHitTime.
    TakeDamage(col.transform);
    lastHitTime = Time.time;
    }
    // If the player doesn't have health, do some stuff, let him fall into the river to reload the level.
    else
    {
    // Find all of the colliders on the gameobject and set them all to be triggers.
    Collider2D[] cols = GetComponents<Collider2D>();
    foreach(Collider2D c in cols)
    {
    c.isTrigger = true;
    }

    // Move all sprite parts of the player to the front
    SpriteRenderer[] spr = GetComponentsInChildren<SpriteRenderer>();
    foreach(SpriteRenderer s in spr)
    {
    s.sortingLayerName = "UI";
    }

    // ... disable user Player Control script
    GetComponent<PlayerControl>().enabled = false;

    // ... disable the Gun script to stop a dead guy shooting a nonexistant bazooka
    GetComponentInChildren<Gun>().enabled = false;

    // ... Trigger the 'Die' animation state
    anim.SetTrigger("Die");
    }
    }
    }
    }


    void TakeDamage (Transform enemy)
    {
    // Make sure the player can't jump.
    playerControl.jump = false;

    // Create a vector that's from the enemy to the player with an upwards boost.
    Vector3 hurtVector = transform.position - enemy.position + Vector3.up * 5f;

    // Add a force to the player in the direction of the vector and multiply by the hurtForce.
    GetComponent<Rigidbody2D>().AddForce(hurtVector * hurtForce);

    // Reduce the player's health by 10.
    health -= damageAmount;

    // Update what the health bar looks like.
    UpdateHealthBar();

    // Play a random clip of the player getting hurt.
    int i = Random.Range (0, ouchClips.Length);
    AudioSource.PlayClipAtPoint(ouchClips, transform.position);
    }


    public void UpdateHealthBar ()
    {
    // Set the health bar's colour to proportion of the way between green and red based on the player's health.
    healthBar.material.color = Color.Lerp(Color.green, Color.red, 1 - health * 0.01f);

    // Set the scale of the health bar to be proportional to the player's health.
    healthBar.transform.localScale = new Vector3(healthScale.x * health * 0.01f, 1, 1);
    }
    }
     
  26. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,498
    Please STOP posting to seven-year-old threads. It's against forum rules.

    The code does NOT MATTER to anyone here and you do NOT NEED a post for null reference.

    Here is why:

    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.

    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
     
  27. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,498
    ALSO: remember Rule #1 about GameObject.Find():

    Rule #1: DO NOT USE GameObject.Find();
     
  28. Ernestio

    Ernestio

    Joined:
    Jul 4, 2021
    Posts:
    1
    Can some body help me with this error
    NullReferenceException: Object reference not set to an instance of an object
    how.OnMouseDown () (at Assets/how.cs:12)
    UnityEngine.SendMouseEventsoSendMouseEvents(Int32)


    here is the code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class how : MonoBehaviour
    {
    public Transform theDest;

    void OnMouseDown() {
    GetComponent<Rigidbody>().useGravity = false;
    this.transform.position = theDest.position;
    this.transform.parent = GameObject.Find("Destination").transform;
    }


    void OnMouseUp()
    {
    this.transform.parent = null;
    GetComponent<Rigidbody>().useGravity = true;
    }





    }

    the error pops up when i try to grab my object by left clicking
     
  29. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,498
    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.

    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
     
  30. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,952
    Closing. There are plenty of answers here and other threads. Please don't just post code and say "help!". The whole point of this forum is that all these answers and information is archived. Everything you need to know about your issue is addressed many times, (several in this thread alone).

    Additionally, please use code tags and read the rules for proper posting etiquette.
     
    Kurt-Dekker and RadRedPanda like this.
Thread Status:
Not open for further replies.