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.