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

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:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GachaSystem : MonoBehaviour
{
    private float probability;
    private int SRCount = 0;
    private int SSRCount = 0;

    public List<Character> R = new List<Character>();
    public List<Character> SR = new List<Character>();
    public List<Character> SSR = new List<Character>();

    public GameObject OneCardDraw;
    public OneCard OC;

    void Start()
    {
        foreach (Character character in GM.gm.charactersDatabase)
        {
            if (character.rarity == _rarity.R)
            {
                R.Add(character);
            }

            if (character.rarity == _rarity.SR)
            {
                SR.Add(character);
            }

            if (character.rarity == _rarity.SSR)
            {
                SSR.Add(character);
            }
        }
    }

    public void OneDrawRegular()
    {
        if (GM.gm.gems - 100 >= 0)
        {
            GM.gm.gems -= 100;
            probability = Random.Range(0f,1f);
            bool isAvailable = true;
            if (probability >= 0.6)
            {
                int index = Random.Range(0, R.Count);
                for (int i = 0; i < GM.gm.availableCharacters.Count; i++)
                {
                    if (GM.gm.availableCharacters[i].characterName == R[index].characterName)
                    {
                        isAvailable = false;
                        break;
                    }
                }

                if (isAvailable)
                {
                    OC.character = R[index];
                    GM.gm.availableCharacters.Add(R[index]);
                    SRCount++;
                    GameObject go = Instantiate(OneCardDraw);
                }
                else
                {
                    GM.gm.gems += 25;
                    SRCount++;
                }
            }

            if (probability >= 0.3 && probability < 0.6)
            {
                int index = Random.Range(0, SR.Count);
                for (int i = 0; i < GM.gm.availableCharacters.Count; i++)
                {
                    if (GM.gm.availableCharacters[i].characterName == SR[index].characterName)
                    {
                        isAvailable = false;
                        break;
                    }
                }

                if (isAvailable)
                {
                    OC.character = SR[index];
                    GM.gm.availableCharacters.Add(SR[index]);
                    SRCount = 0;
                    GameObject go = Instantiate(OneCardDraw);
                }
                else
                {
                    GM.gm.gems += 25;
                    SRCount = 0;
                }
            }

            if (probability <= 0.2)
            {
                int index = Random.Range(0, SSR.Count);
                for (int i = 0; i < GM.gm.availableCharacters.Count; i++)
                {
                    if (GM.gm.availableCharacters[i].characterName == SSR[index].characterName)
                    {
                        isAvailable = false;
                        break;
                    }
                }

                if (isAvailable)
                {
                    OC.character = SSR[index];
                    GM.gm.availableCharacters.Add(SSR[index]);
                    SRCount++;
                    GameObject go = Instantiate(OneCardDraw);
                }
                else
                {
                    GM.gm.gems += 25;
                    SRCount++;
                }
              
            }

            else if (SRCount == 10)
            {
                int index = Random.Range(0, SR.Count);
                //For now I'll do the same thing but I want to make it that it'll bring one that's available
                for (int i = 0; i < GM.gm.availableCharacters.Count; i++)
                {
                    if (GM.gm.availableCharacters[i].characterName == SR[index].characterName)
                    {
                        isAvailable = false;
                        break;
                    }
                }

                if (isAvailable)
                {
                    OC.character = SR[index];
                    GM.gm.availableCharacters.Add(SR[index]);
                    SRCount = 0;
                    GameObject go = Instantiate(OneCardDraw);
                }
                else
                {
                    GM.gm.gems += 25;
                    SRCount = 0;
                }
            }
        }
    }
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System;

public class OneCard : MonoBehaviour
{
    public Image SSRPic;
    public Image SRPic;
    public Image Rarity;
    public Image Element;
    public TMP_Text characterName;
    public GachaSystem GS;

    public Character character;
    // Start is called before the first frame update
    void Start()
    {
        if (character.rarity == _rarity.SSR)
        {
            SSRPic.enabled = true;
            SSRPic.sprite = character.pic;
        }
        else
        {
            SRPic.enabled = true;
            SRPic.sprite = character.pic;
        }
        Rarity.sprite = character.rarityIcon;
        Element.sprite = character.elementalType.icon;
        characterName.text = character.characterName;
    }
}

The problem is with the character inside the OneCard script and I don’t get why it costs issues.

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:

Step by step, break it down, find the problem.

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.

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.