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 refernce null but its suppose to be null;

Discussion in 'Scripting' started by KrazyWulf1983, Mar 3, 2021.

  1. KrazyWulf1983

    KrazyWulf1983

    Joined:
    May 5, 2014
    Posts:
    12
    so object reference is null but yet unity throws an error... its suppose to be null until an item is placed in inventory or dragged around placed in equipment section.. so how do i stop the constant error spamming?

    so i have 2 null refernce erros and 1 combatscript not working.. feel free to help with all or just 1

    upload_2021-3-3_10-59-42.png

    upload_2021-3-3_11-10-52.png
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public enum ItemType
    7. {
    8.     Food,
    9.     Potion,
    10.     Helmet,
    11.     Weapon,
    12.     Shield,
    13.     Boots,
    14.     Chest,
    15.     Pants,
    16.     Ore,
    17.     Crystal,
    18.     Default
    19. }
    20.  
    21. public enum Attributes
    22. {
    23.     Dex,
    24.     Str,
    25.     Char,
    26.     Con,
    27.     Int,
    28. }
    29.  
    30.  
    31. public abstract class ItemObject : ScriptableObject
    32. {
    33.     public int Id;
    34.     public Sprite uiDisplay;
    35.     public ItemType  type;
    36.     [TextArea(15, 20)]public string description;
    37.     public ItemBuff[] buffs;
    38.  
    39.     public Item CreateItem()
    40.     {
    41.         Item newItem = new Item(this);
    42.         return newItem;
    43.     }
    44.  
    45.  
    46. }
    47.  
    48. [System.Serializable]
    49. public class Item
    50. {
    51.     public string Name;
    52.     public int Id;
    53.     public ItemBuff[] buffs;
    54.     public Item()
    55.     {
    56.         Name = "";
    57.         Id = -1;
    58.     }
    59.     public Item(ItemObject item)
    60.     {
    61.         Name = item.name;
    62.         Id = item.Id;
    63.         buffs = new ItemBuff[item.buffs.Length];
    64.         for (int i = 0; i < buffs.Length; i++)
    65.         {
    66.             buffs[i] = new ItemBuff(item.buffs[i].min, item.buffs[i].max)
    67.             {attributes = item.buffs[i].attributes};
    68.         }
    69.     }
    70. }
    71. [System.Serializable]
    72. public class ItemBuff
    73. {
    74.     public Attributes attributes;
    75.     public int value;
    76.     public int min;
    77.     public int max;
    78.     public ItemBuff(int _min, int _max)
    79.     {
    80.         min = _min;
    81.         max = _max;
    82.         GenerateValue();
    83.     }
    84.     public void GenerateValue()
    85.     {
    86.         value = UnityEngine.Random.Range(min, max);
    87.     }
    88. }
    89.  

    ISSUE 2
    i have another null reference exception how ever i have the object in inspector referenced
    combat script wont change health and or says death when health is above 0

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class OreNodeStats : MonoBehaviour
    6. {
    7.     public int oreHealth;
    8.     public GameObject oreItem;
    9.  
    10.     public void Update()
    11.     {
    12.         if (oreHealth <= 0)
    13.         {
    14.             Debug.Log("death");
    15.             var obj = Instantiate(oreItem, transform.position, Quaternion.identity);
    16.             Destroy(this);
    17.         }
    18.     }
    19.     public void TakeDamage(int damage)
    20.     {
    21.         oreHealth -= damage;
    22.         Debug.Log("damage TAKEN");
    23.     }
    24. }

    upload_2021-3-3_10-58-30.png
     
    Last edited: Mar 3, 2021
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    So your errors when you click on them should show a longer description which includes the line of code it occurred. One of them does show it in GroundItem.cs at line 17 (which you did not include the code for in your post).

    The error you're getting is not just because you didn't set the variable to some instance... having something null is perfectly fine.

    It's that you are attempting to act upon that variable.

    Code (csharp):
    1.  
    2. GameObject obj = null; //this is fine
    3. obj.transform.position = Vector3.zero; //this is not ok since the previous line set it null
    4.  
    Go to the lines of codes that the errors relate to and see what you're trying to reference/modify and you should first check if it's null or not before doing it.

    Code (csharp):
    1.  
    2. GameObject obj = null; //this is fine
    3. if (obj != null) obj.transform.position = Vector3.zero; //this is now ok because you validate if the object is null or not first
    4.  
     
    PraetorBlue likes this.
  3. KrazyWulf1983

    KrazyWulf1983

    Joined:
    May 5, 2014
    Posts:
    12

    i linked the code in second imagine also here .. i was following a you tube tutorial.. stuff shouldent be triggering just randomly feels odd actually.. should only be triggering on item change pickup or inventory activity.. yes its checked every frame but i figured that was ok if nothing to react apon.. plus the tutorial said we would change it later.. the developer of it no longer helps so i came here..


    as for the gamkeobject being null i set it in the inspector the obj = transform.. so why it still screaming null?
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Please use Code Tags, not images - read the very first post in this forum.

    You are using GetComponentInChildren<SpriteRenderer>() without checking if it is null before accessing sprite. You are also not checking if item is null before accessing uiDisplay.

    Use Debug.Log() to write their values to the console before accessing to ensure they are set
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,757
    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    For null reference, 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.

    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.
     
    Schneider21 likes this.
  6. KrazyWulf1983

    KrazyWulf1983

    Joined:
    May 5, 2014
    Posts:
    12

    from what i can tell its being set as null ebcause of uiDisplay in ItemObject abstract class.. however i set it in unity inspector to be a defult imagie.. yet its still calling null?.. yes i dont know how to use debug.log as effective as most others. im only a month into my unity experiance
     
  7. KrazyWulf1983

    KrazyWulf1983

    Joined:
    May 5, 2014
    Posts:
    12
    figured the code screanshot would help udnerstand the refernce unity was making to the code and highlighted selection
     
  8. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    It doesn't really help understanding; images are not searchable, quotable, and usually have a different resolution from the text we use to display the remainder of the post. If you want people to help you, it helps making it easy :)

    So, to re-iterate: you are using unsafe code, and you should make sure that you aren't accessing null objects. Why don't you insert the two debug.logs into the code to check if the variables are null, and go from there?

    Good luck,
    -ch
     
  9. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If a reference variable is supposed to sometimes be null, check if it is null. If it is null, then don't use it until it is not null. If it is not null, then you are fine to use it. It is really that simple. I'd write an example, but I can't edit code from a screen shot.
     
  10. KrazyWulf1983

    KrazyWulf1983

    Joined:
    May 5, 2014
    Posts:
    12
    so i used debug.log(item.uiDisplay); kjuts out of randomness trying to figure out how to check it with debug since no one really says how and every guide peopel post is so genererk. it said im missing keys for a buncha new items i made... now i gota figure out how to update the dictonaery my database is uptodate