Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question NullReferenceException

Discussion in 'Prefabs' started by Daeshimaru, Jun 12, 2021.

  1. Daeshimaru

    Daeshimaru

    Joined:
    May 11, 2021
    Posts:
    2
    Hello, i have a class called RingWorld that allow me to instantiate prefabs in my world:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RingWorld : MonoBehaviour
    6. {
    7.     public static RingWorld SpawnHealthRingWorld(Vector3 position)
    8.     {
    9.         Transform transform = Instantiate(ItemAssets.Instance.PrefabHealthRing, position, ItemAssets.Instance.PrefabHealthRing.transform.rotation);
    10.  
    11.         RingWorld healthRingWorld = transform.GetComponent<RingWorld>();
    12.  
    13.         return healthRingWorld;
    14.     }
    15.  
    16.     /* public static RingWorld SpawnManaRingWorld(Vector3 position)
    17.     {
    18.         Transform manaRingTransform = Instantiate(ItemAssets.Instance.PrefabManaRing, position, ItemAssets.Instance.PrefabManaRing.transform.rotation);
    19.  
    20.         RingWorld manaRingWorld = manaRingTransform.GetComponent<RingWorld>();
    21.  
    22.         return manaRingWorld;
    23.     } */
    24.  
    25.     public void DestroyRingSelf() //class to destroy the ring passing a gameObject in an intern Destroy() method
    26.     {
    27.         Destroy(gameObject);
    28.     }
    29. }
    30.  

    I reference this class in my main script called ThirdPersonMovement, here:

    Code (CSharp):
    1. private RingWorld ringWorld; //this is the reference at the top of the script
    2.  
    3. private void UseItem(Item item) //function to use the items
    4.     {
    5.         switch (item.itemType)
    6.         {
    7.             case Item.ItemType.HealthPotion: //in case that i use health potion...
    8.  
    9.                 ringWorld = FindObjectOfType(typeof(RingWorld)) as RingWorld; //here i use a trick to initialize my MonoBehaviour class (RingWorld) to use a function from her
    10.                 inventory.RemoveItem(new Item { itemType = Item.ItemType.HealthPotion, amount = 1 }); //function to destroy the item
    11.  
    12.                 RingWorld.SpawnHealthRingWorld(GameObject.FindGameObjectWithTag("Player").transform.position); //finding the gameobject where i want the prefab spawn at
    13.                 new WaitForSeconds(3f); //waiting for three seconds after it spawns
    14.                 ringWorld.DestroyRingSelf(); //and this is the line of the problem. i don't understand it, so i would like if you can see and solve the problem
    15.                 }
    16.              
    17.                 break;
    18.  
    19.             /*case Item.ItemType.ManaPotion:
    20.                 FlashBlue();
    21.                 inventory.RemoveItem(new Item { itemType = Item.ItemType.ManaPotion, amount = 1 });
    22.                 break; */
    23.         }
    24.     }
     
    Last edited: Jun 12, 2021
  2. eMsylf

    eMsylf

    Joined:
    Mar 9, 2018
    Posts:
    10
    A NullReferenceException means here that the variable ringWorld is null, meaning that FindObjectOfType on line 9 did not find a RingWorld object. It is looking for an instance of the object that would need to be attached to a game object in the scene. Is it possible that there is no such game object?