Search Unity

New to Events - Enemy script keeps throw nullrefrenceexception

Discussion in 'Scripting' started by BlackBlood_Dev, Jan 18, 2022.

  1. BlackBlood_Dev

    BlackBlood_Dev

    Joined:
    May 30, 2018
    Posts:
    2
    I'm fairly new to events and C# programming in general. I decided to go with a simple event based system so that I don't end referencing everything in my project. But when I hooked up my enemy script to my event system, it keeps throwing a null reference exception. Any help or advice would be greatly appreciated.

    Event System code
    ____________________________________
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5.  
    6. public class EventManager : MonoBehaviour
    7. {
    8.     public static EventManager current;
    9.     private void Awake()
    10.     {
    11.         current = this;
    12.     }
    13.     public event Action onPlayerSeen;
    14.     public void PlayerSeen()
    15.     {
    16.         if(onPlayerSeen != null)
    17.         {
    18.             onPlayerSeen();
    19.         }
    20.     }
    21.     public event Action onShootProjectile;
    22.     public void ShootProjectile()
    23.     {
    24.         if(onShootProjectile != null)
    25.         {
    26.             onShootProjectile();
    27.         }
    28.     }
    29.     public event Action onLifeLost;
    30.     public void LifeLost()
    31.     {
    32.         if(onLifeLost != null)
    33.         {
    34.             onLifeLost();
    35.         }
    36.     }
    37.     public event Action onGameOver;
    38.     public void GameOver()
    39.     {
    40.         if(onGameOver != null)
    41.         {
    42.             onGameOver();
    43.         }
    44.  
    Enemy code
    ____________________________________________
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Enemy : MonoBehaviour
    6. {
    7.     private Rigidbody enemyRigidbody;
    8.     private float moveSpeed,rotateSpeed;
    9.     [SerializeField] private GameObject targetPoint;
    10.     [SerializeField] private GameObject projectile;
    11.     void start()
    12.     {
    13.         enemyRigidbody = GetComponent<Rigidbody>();
    14.         moveSpeed = 10f;
    15.     }
    16.     void OnEnable()
    17.     {
    18.         EventManager.current.onPlayerSeen += PlayerSeen;
    19.     }
    20.     void OnDisable()
    21.     {
    22.         EventManager.current.onPlayerSeen -= PlayerSeen;
    23.     }
    24.     void Update()
    25.     {
    26.         transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
    27.     }
    28.     private void PlayerSeen()
    29.     {
    30.         Debug.Log("Player seen by " + gameObject.name);
    31.         Instantiate(projectile, targetPoint.transform.position, Quaternion.identity);
    32.     }
    33. }
    34.  
    Trigger code
    _______________________________________________
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Trigger : MonoBehaviour
    6. {
    7.    void OnTriggerEnter(Collider other)
    8.    {
    9.        if(other.gameObject.tag == "Player")
    10.        EventManager.current.PlayerSeen();
    11.    }
    12. }
    13.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Nothing special about that.

    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