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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

NullReferenceException: Object reference not set to an instance of an object Error

Discussion in 'Scripting' started by noa1359, Apr 6, 2021.

  1. noa1359

    noa1359

    Joined:
    Apr 24, 2019
    Posts:
    2
    Hello everyone,
    I wrote those 2 codes: And I get the error:
    NullReferenceException: Object reference not set to an instance of an object

    I really don't get why I get this error and how I should fix this error: Here's my two codes below:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GachaSystem : MonoBehaviour
    6. {
    7.     private float probability;
    8.     private int SRCount = 0;
    9.     private int SSRCount = 0;
    10.  
    11.     public List<Character> R = new List<Character>();
    12.     public List<Character> SR = new List<Character>();
    13.     public List<Character> SSR = new List<Character>();
    14.  
    15.     public GameObject OneCardDraw;
    16.     public OneCard OC;
    17.  
    18.     void Start()
    19.     {
    20.         foreach (Character character in GM.gm.charactersDatabase)
    21.         {
    22.             if (character.rarity == _rarity.R)
    23.             {
    24.                 R.Add(character);
    25.             }
    26.  
    27.             if (character.rarity == _rarity.SR)
    28.             {
    29.                 SR.Add(character);
    30.             }
    31.  
    32.             if (character.rarity == _rarity.SSR)
    33.             {
    34.                 SSR.Add(character);
    35.             }
    36.         }
    37.     }
    38.  
    39.     public void OneDrawRegular()
    40.     {
    41.         if (GM.gm.gems - 100 >= 0)
    42.         {
    43.             GM.gm.gems -= 100;
    44.             probability = Random.Range(0f,1f);
    45.             bool isAvailable = true;
    46.             if (probability >= 0.6)
    47.             {
    48.                 int index = Random.Range(0, R.Count);
    49.                 for (int i = 0; i < GM.gm.availableCharacters.Count; i++)
    50.                 {
    51.                     if (GM.gm.availableCharacters[i].characterName == R[index].characterName)
    52.                     {
    53.                         isAvailable = false;
    54.                         break;
    55.                     }
    56.                 }
    57.  
    58.                 if (isAvailable)
    59.                 {
    60.                     OC.character = R[index];
    61.                     GM.gm.availableCharacters.Add(R[index]);
    62.                     SRCount++;
    63.                     GameObject go = Instantiate(OneCardDraw);
    64.                 }
    65.                 else
    66.                 {
    67.                     GM.gm.gems += 25;
    68.                     SRCount++;
    69.                 }
    70.             }
    71.  
    72.             if (probability >= 0.3 && probability < 0.6)
    73.             {
    74.                 int index = Random.Range(0, SR.Count);
    75.                 for (int i = 0; i < GM.gm.availableCharacters.Count; i++)
    76.                 {
    77.                     if (GM.gm.availableCharacters[i].characterName == SR[index].characterName)
    78.                     {
    79.                         isAvailable = false;
    80.                         break;
    81.                     }
    82.                 }
    83.  
    84.                 if (isAvailable)
    85.                 {
    86.                     OC.character = SR[index];
    87.                     GM.gm.availableCharacters.Add(SR[index]);
    88.                     SRCount = 0;
    89.                     GameObject go = Instantiate(OneCardDraw);
    90.                 }
    91.                 else
    92.                 {
    93.                     GM.gm.gems += 25;
    94.                     SRCount = 0;
    95.                 }
    96.             }
    97.  
    98.             if (probability <= 0.2)
    99.             {
    100.                 int index = Random.Range(0, SSR.Count);
    101.                 for (int i = 0; i < GM.gm.availableCharacters.Count; i++)
    102.                 {
    103.                     if (GM.gm.availableCharacters[i].characterName == SSR[index].characterName)
    104.                     {
    105.                         isAvailable = false;
    106.                         break;
    107.                     }
    108.                 }
    109.  
    110.                 if (isAvailable)
    111.                 {
    112.                     OC.character = SSR[index];
    113.                     GM.gm.availableCharacters.Add(SSR[index]);
    114.                     SRCount++;
    115.                     GameObject go = Instantiate(OneCardDraw);
    116.                 }
    117.                 else
    118.                 {
    119.                     GM.gm.gems += 25;
    120.                     SRCount++;
    121.                 }
    122.              
    123.             }
    124.  
    125.             else if (SRCount == 10)
    126.             {
    127.                 int index = Random.Range(0, SR.Count);
    128.                 //For now I'll do the same thing but I want to make it that it'll bring one that's available
    129.                 for (int i = 0; i < GM.gm.availableCharacters.Count; i++)
    130.                 {
    131.                     if (GM.gm.availableCharacters[i].characterName == SR[index].characterName)
    132.                     {
    133.                         isAvailable = false;
    134.                         break;
    135.                     }
    136.                 }
    137.  
    138.                 if (isAvailable)
    139.                 {
    140.                     OC.character = SR[index];
    141.                     GM.gm.availableCharacters.Add(SR[index]);
    142.                     SRCount = 0;
    143.                     GameObject go = Instantiate(OneCardDraw);
    144.                 }
    145.                 else
    146.                 {
    147.                     GM.gm.gems += 25;
    148.                     SRCount = 0;
    149.                 }
    150.             }
    151.         }
    152.     }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6. using System;
    7.  
    8. public class OneCard : MonoBehaviour
    9. {
    10.     public Image SSRPic;
    11.     public Image SRPic;
    12.     public Image Rarity;
    13.     public Image Element;
    14.     public TMP_Text characterName;
    15.     public GachaSystem GS;
    16.  
    17.     public Character character;
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         if (character.rarity == _rarity.SSR)
    22.         {
    23.             SSRPic.enabled = true;
    24.             SSRPic.sprite = character.pic;
    25.         }
    26.         else
    27.         {
    28.             SRPic.enabled = true;
    29.             SRPic.sprite = character.pic;
    30.         }
    31.         Rarity.sprite = character.rarityIcon;
    32.         Element.sprite = character.elementalType.icon;
    33.         characterName.text = character.characterName;
    34.     }
    35. }
    36.  
    The problem is with the character inside the OneCard script and I don't get why it costs issues.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    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.
     
  3. noa1359

    noa1359

    Joined:
    Apr 24, 2019
    Posts:
    2
    Hi, thank you for the offer to help.
    I do understand this error, I just don't understand what's null inside of my character, since I did the same thing by moving one character from one character to the next many times.

    I'll try to imply the links that you've sent to my code, thank you.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Look closely at the error. It tells you the line. If that's not enough, break the line down:

    How to break down hairy lines of code:

    http://plbm.com/?p=248

    If you still can't figure it out, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.

    If you are running a mobile device you can also see the console output. Google for how.