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

Unable to make enemy affect player since the object isn't set to reference

Discussion in 'Scripting' started by Vib3-Ch3cked, Mar 30, 2022.

  1. Vib3-Ch3cked

    Vib3-Ch3cked

    Joined:
    Jan 24, 2022
    Posts:
    7
    I'm working on a small game project where an enemy is moving towards the player. Coming in contact with the enemy despawns the player and causes a game over. The game is pretty much functional aside from the enemy, as the player passes through the enemy rather than triggering a game over.

    This is the error message I got:
    NullReferenceException: Object reference not set to an instance of an object
    GameManager.Start () (at Assets/Scripts/GameManager.cs:20)

    It appears to be an issue in the game manager script, but I'll post the obstacle script too if that helps.

    GAME MANAGER
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6. using UnityEngine.SceneManagement;
    7.  
    8. public class GameManager : MonoBehaviour
    9. {
    10.  
    11.     public int score;
    12.     public int scoreTarget;
    13.     public TMP_Text scoreText;
    14.     public TMP_Text winText;
    15.  
    16.     //***ABOVE IS FOR WIN***
    17.  
    18.     private void Start()
    19.     {
    20.         winText.gameObject.SetActive(false);
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.         // If the esc key was pressed and released...
    27.         if (Input.GetKeyUp(KeyCode.Escape) == true)
    28.         {
    29.             // Quit the game (only works with built games)
    30.             Application.Quit();
    31.         }
    32.  
    33.         //****BOTTOM CODE RELOADS A SCENE AFTER WINNING****
    34.         if (Input.GetKeyUp(KeyCode.Space) == true)
    35.         {
    36.             // Reload the scene
    37.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    38.         }
    39.  
    40.  
    41.     }
    42.  
    43.     //****USE FOR GAME OVER****
    44.     public void Lose()
    45.     {
    46.         winText.gameObject.SetActive(true);
    47.         winText.text = "THERE IS NO ESCAPE\npress space to restart";
    48.     }
    49. }
    ENEMY/OBSTACLE

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class ObstacleCheck : MonoBehaviour
    7. {
    8.     private float dirX;
    9.     private float moveSpeed;
    10.     private Rigidbody2D rb;
    11.     private Vector3 localScale;
    12.  
    13.     void Start()
    14.     {
    15.         localScale = transform.localScale;
    16.         rb = GetComponent<Rigidbody2D>();
    17.         dirX = 1f;
    18.         moveSpeed = 3f;
    19.     }
    20.  
    21.     private void OnCollisionEnter2D(Collision2D collision)
    22.     {
    23.         if (collision.gameObject.tag == "Player")
    24.         {
    25.             SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    26.         }
    27.     }
    28.  
    29.     void FixedUpdate()
    30.     {
    31.         rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);
    32.     }
    33. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    It actually doesn't even matter what you're doing or what code you post.

    The answer is always the same.

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Steps to success:
    - Identify what is null
    - Identify why it is null
    - Fix that
     
  3. Vib3-Ch3cked

    Vib3-Ch3cked

    Joined:
    Jan 24, 2022
    Posts:
    7
    A little confused on how I should break down the hairy code that's causing the problems. The example provided is a bit different to what my problematic code line is (I think it's 20 judging by the error message, but I could be wrong)
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Did you put a Debug.Log() immediately prior to the scene load to see what string you're ultimately giving it?