Search Unity

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

Discussion in 'Scripting' started by renatoemeric11, Jun 9, 2021.

  1. renatoemeric11

    renatoemeric11

    Joined:
    Jun 2, 2021
    Posts:
    2
    Hi I'm pretty new in Unity and trying to solve this problem I'm having which is the NullReferenceExcpetion, I don't know much of coding. So if someone help its really appriciated.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Vaccine : MonoBehaviour
    6. {
    7.     public GameObject pickupEffect;
    8.     public Score currentScore;
    9.     public float BonusScore;
    10.     private float immuneTime = 5.0f;
    11.     public PlayerMotor player;
    12.     private IEnumerator tookVaccineCo;
    13.  
    14.     private void Start()
    15.     {
    16.         currentScore = FindObjectOfType<Score>();
    17.         tookVaccineCo = TookVaccine();
    18.         player = GetComponent<PlayerMotor>();
    19.     }
    20.  
    21.     void OnTriggerEnter(Collider other)
    22.     {
    23.         if (other.CompareTag("Player"))
    24.         {
    25.             var player = other.GetComponent<PlayerMotor>();
    26.             Pickup();
    27.             StopCoroutine(tookVaccineCo);
    28.             StartCoroutine(tookVaccineCo);
    29.         }
    30.     }
    31.     IEnumerator TookVaccine()
    32.     {
    33.         player.isImmune = true;
    34.         yield return new WaitForSeconds(immuneTime);
    35.         player.isImmune = false;
    36.     }
    37.  
    38.     void Pickup()
    39.     {
    40.         currentScore.CurrentScore = currentScore.CurrentScore + BonusScore;
    41.         //apply effect for player
    42.  
    43.  
    44.         //remove power up object
    45.         Destroy(gameObject);
    46.     }
    47. }
    this is my code on the vaccine part
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    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
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I'll give you +1 for using code tags, but -1 for not sharing your exact error! Always copy and paste the error with the stack trace as it tells you exactly what line your error occurs on. But, if you look at that line and reference @Kurt-Dekker post, you can probably solve it without any additional feedback. But if you are still stuck, post the error with the line number and I'll point you in the right direction.