Search Unity

Object reference not set to an instance of an object

Discussion in 'Scripting' started by cocparas7, Jul 30, 2021.

  1. cocparas7

    cocparas7

    Joined:
    Feb 6, 2021
    Posts:
    7
    So I want make sound when my enemy collides with player(Enemy gets destroy) I made so far an it gives me error. Error is in enemy code when i tried to make sound(line 61)My English is bad sorry...

    Code (CSharp):
    1. public class Enemy_Target : MonoBehaviour
    2. {
    3.  
    4.     public float speed;
    5.     private Transform player;
    6.     Health_Player CodeHp;
    7.     public float damage;
    8.     public GameObject boomeffect;
    9.     SoundCode _codesound;
    10.  
    11.     // public GameObject Boomeffect;
    12.     // Start is called before the first frame update
    13.     private void Awake()
    14.     {
    15.    
    16.     }
    17.     void Start()
    18.     {
    19.      
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         //not null
    26.         if (player != null)
    27.         {
    28.            
    29.             transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
    30.          
    31.         }
    32.  
    33.     }
    34.     private void OnTriggerEnter2D(Collider2D other)
    35.     {
    36.         //set
    37.         if (other.CompareTag("Player"))
    38.         {
    39.             player = other.transform;
    40.          
    41.         }
    42.     }
    43.     private void OnTriggerExit2D(Collider2D other)
    44.     {
    45.         //remove
    46.         if (other.CompareTag("Player"))
    47.         {
    48.             player = null;
    49.  
    50.         }
    51.     }
    52.     private void OnCollisionEnter2D(Collision2D collision)
    53.     {
    54.         if (collision.gameObject.tag == "Player")
    55.         {
    56.             //give damage
    57.             CodeHp = collision.transform.GetComponent<Health_Player>();
    58.             CodeHp.health = CodeHp.health - damage;
    59.  
    60.          
    61.             _codesound.audiosource.PlayOneShot(_codesound.boomsound); //here error
    62.  
    63.             Destroy(gameObject);
    64.            GameObject EFFect= Instantiate(boomeffect, transform.position, Quaternion.identity);
    65.             Destroy(EFFect, 1f);
    66.         }
    67.     }
    68. }
    2ND code
    Code (CSharp):
    1. public class SoundCode : MonoBehaviour
    2. {
    3.     public AudioSource audiosource;
    4.     public AudioClip boomsound;
    5.  
    6.  
    7.     private void Awake()
    8.     {
    9.      
    10.     }
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         audiosource = this.GetComponent<AudioSource>();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         if (audiosource = null)
    22.         {
    23.          
    24.             Debug.Log("No audiosouce pls add ");
    25.         }
    26.     }
    27.  
    28.  
    29.  
    30. }
    31.  
     
    Last edited: Jul 30, 2021
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    _codesound
    is a private variable and you never assign it anywhere. Therefore
    _codesound
    will always be null, causing a NullReferenceException any time you try to use it.

    You have to assign a value somewhere to
    _codesound
    , either by making it public and assigning it in the inspector, or by assigning a value somewhere in your code, e.g.:
    _codesound = <something>;
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    This is the same thing as a null reference error. It means you have nothing assigned.
    _codesound has no value.

    This is the simpliest error to solve.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    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
     
  5. cocparas7

    cocparas7

    Joined:
    Feb 6, 2021
    Posts:
    7
    void Start()
    {
    _codesound = GameObject.FindGameObjectWithTag("SoundCont").transform.GetComponent<SoundCode>();
    }
    so i added this sitll it gives error when enemy collid with player sorry i am new to unity and coding. I put this line to Debug.Log(_codesound!=null); //true
     
  6. cocparas7

    cocparas7

    Joined:
    Feb 6, 2021
    Posts:
    7
    Update : now its working i created same sound code and it worked Thank you(I think problem was i had added to other object maybe.... i dont know)
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Agreed. Null references do not even require ever posting to the forum even once because that will not fix the problem.

    The problem and the fix for null reference are ALWAYS the same three steps. ALWAYS:

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


    There is simply no other thing you can do or accomplish. It is the only answer.