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

Object reference not set to an instance of an Object

Discussion in 'Scripting' started by murgntheurgn, May 1, 2020.

  1. murgntheurgn

    murgntheurgn

    Joined:
    Oct 12, 2019
    Posts:
    8
    Hi there! Sorry if this question is dumb but im relatively new to C# and I've come across an error and I'm not quite sure how to fix it, all help is appreciated. :) I'm trying to call the function OnPlayer1Kill and OnPlayer2Kill from another script and that's the line thats telling me the error is on. If anyone is able to help my code is below. Thank You!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DestroyWhenTouchingDeath : MonoBehaviour
    6. {
    7.     public string strTag;
    8.     public GameObject player;
    9.     public string whatPlayer;
    10.  
    11.     public GameObject Spawner;
    12.     private Spawner script;
    13.  
    14.  
    15.     private void Start()
    16.     {
    17.         script = Spawner.GetComponent<Spawner>();
    18.     }
    19.  
    20.     private void OnCollisionEnter(Collision collision)
    21.     {
    22.         if (collision.collider.tag == strTag)
    23.         {
    24.             Destroy(player);
    25.         }
    26.  
    27.         if (whatPlayer == "Player1" && collision.collider.tag == strTag)
    28.         {
    29.             script.OnPlayer1Kill();
    30.         }
    31.  
    32.         if (whatPlayer == "Player2" && collision.collider.tag == strTag)
    33.         {
    34.             script.OnPlayer2Kill();
    35.         }
    36.     }
    37.  
    38. }
    39.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Spawner : MonoBehaviour
    6. {
    7.     public Transform[] spawnLocations;
    8.     public GameObject[] whatToSpawnPrefab;
    9.  
    10.     public void OnPlayer1Kill()
    11.     {
    12.         Vector3 position = spawnLocations[0].position;
    13.  
    14.         GameObject newGameObject = Instantiate(whatToSpawnPrefab[0]);
    15.  
    16.         newGameObject.transform.position = position;
    17.     }
    18.  
    19.     public void OnPlayer2Kill()
    20.     {
    21.         Vector3 position = spawnLocations[1].position;
    22.  
    23.         GameObject newGameObject = Instantiate(whatToSpawnPrefab[1]);
    24.  
    25.         newGameObject.transform.position = position;
    26.     }
    27. }
    28.  
     
  2. yannisk

    yannisk

    Joined:
    Jul 10, 2019
    Posts:
    5
    This error means that you try to access some informations but you haven't declared.

    When applying a script at a GameObject, through the Inspector you will see some areas that need to be filled.

    For example on the second script, spawnLocations : drag and drop your spawn locations GameObjects, for whatToSpawnPrefab: drag and drop the GameObject you want to be spawned .

    The same goes for the first script relatively.
     
  3. Zero_Xue

    Zero_Xue

    Joined:
    Apr 18, 2012
    Posts:
    126
    Looking at it am guessing either A you havent but anything in "whatToSpawnPrefab" inside the inspector of your gameObject or B you havent set the "Spawner" gameobject inside the inspector =)
     
  4. murgntheurgn

    murgntheurgn

    Joined:
    Oct 12, 2019
    Posts:
    8
    Thank you! It turned out that I had the first script disabled haha, thanks for your help
     
  5. yannisk

    yannisk

    Joined:
    Jul 10, 2019
    Posts:
    5
    glad I could help :)