Hi Guys, This is ryan and I hope you can help me. Here is the video for you to see the complete details about the problem. Thank you.
Here is the error message
NullReferenceException: Object reference not set to an instance of an object
GameManagerLevel3.initializeCards () (at Assets/Scripts/GameManagerLevel3.cs:34)
GameManagerLevel3.Update () (at Assets/Scripts/GameManagerLevel3.cs:20)
Here is the complete project with errors, maybe you need it. By the way im using unity 2017.1.1
There is probably some type of connection in the game manager to the cards you deleted. You can’t just delete them and expect it to work. Keep studying and you’ll figure it out. The error is probably that it was looking for the deleted cards but they weren’t there anymore. So there wasn’t an object reference. Go through the tutorial again and you will probably figure it out.
I haven’t looked at the project, but since you deleted stuff from the scene, if you had a reference to it somewhere, that reference now points to null. I would assume it is a list or array, if the tutorial is decent, but if it’s been populated in the inspector, you’ll have empty slots now (which would be null) so you have to reduce your array/list size.
Hi Fireside, on the tutorial, there is only one level that he created, now what i did was i put some main menu and submenu (where you can choose difficulty level) on the game and there is the result, As of this writing im still studying C#. Thank you for you reply sir.
Hello, thank you for your reply, here is the code inside this project
GameManager
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;
public class GameManagerLevel3 : MonoBehaviour {
public Sprite[] cardFace;
public Sprite cardBack;
public GameObject[] cards;
public Text matchText;
private bool _init = false;
private int _matches = 13;
// Update is called once per frame
void Update() {
if (!_init)
initializeCards();
if (Input.GetMouseButtonUp(0))
checkCards();
}
void initializeCards() {
for (int id = 0; id < 2; id++) {
for (int i = 1; i < 14; i++) {
bool test = false;
int choice = 0;
while (!test) {
choice = Random.Range(0, cards.Length);
test = !(cards[choice].GetComponent<Card>().initialized);
}
cards[choice].GetComponent<Card>().cardValue = i;
cards[choice].GetComponent<Card>().initialized = true;
}
}
foreach (GameObject c in cards)
c.GetComponent<Card>().setupGraphics();
if (!_init)
_init = true;
}
public Sprite getCardBack(){
return cardBack;
}
public Sprite getCardFace(int i)
{
return cardFace[i - 1];
}
void checkCards()
{
List<int> c = new List<int>();
for (int i = 0; i < cards.Length; i++)
{
if (cards[i].GetComponent<Card>().state == 1)
c.Add(i);
}
if (c.Count == 2)
cardComparison(c);
}
void cardComparison(List<int> c)
{
Card.DO_NOT = true;
int x = 0;
if (cards[c[0]].GetComponent<Card>().cardValue == cards[c[1]].GetComponent<Card>().cardValue) {
x = 2;
_matches--;
matchText.text = "Number of Matches: " + _matches;
if (_matches == 0)
SceneManager.LoadScene("Menu");
}
for (int i = 0; i < c.Count; i++)
{
cards[c[i]].GetComponent<Card>().state = x;
cards[c[i]].GetComponent<Card>().falseCheck();
}
}
}
Card.cs
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Card : MonoBehaviour
{
public static bool DO_NOT = false;
[SerializeField]
private int _state;
[SerializeField]
private int _cardValue;
[SerializeField]
private bool _initialized = false;
private Sprite _cardBack;
private Sprite _cardFace;
private GameObject _manager;
void Start()
{
_state = 1;
_manager = GameObject.FindGameObjectWithTag("Manager");
}
public void setupGraphics()
{
_cardBack = _manager.GetComponent<GameManagerLevel3>().getCardBack();
_cardFace = _manager.GetComponent<GameManagerLevel3>().getCardFace(_cardValue);
flipCard();
}
public void flipCard() {
if (_state == 0)
_state = 1;
else if (_state == 1)
_state = 0;
if (_state == 0 && !DO_NOT)
GetComponent<Image>().sprite = _cardBack;
else if (_state == 1 && !DO_NOT)
GetComponent<Image>().sprite = _cardFace;
}
public int cardValue
{
get { return _cardValue; }
set { _cardValue = value; }
}
public int state
{
get { return _state; }
set { _state = value; }
}
public bool initialized
{
get { return _initialized; }
set { _initialized = value; }
}
public void falseCheck()
{
StartCoroutine(pause());
}
IEnumerator pause()
{
yield return new WaitForSeconds(1);
if (_state == 0)
GetComponent<Image>().sprite = _cardBack;
else if (_state == 1)
GetComponent<Image>().sprite = _cardFace;
DO_NOT = false;
}
}
All I can tell you is that there is a gameobject array called cards in the game manager and it probably isn’t finding what’s needed. I don’t see where it was populated, but when you deleted the cards, you probably didn’t change that array, so you got the errors. I think, if this is your first game, it would be a good idea to move on to another tutorial because you may not be ready to make the changes necessary on this one. Just leave it at one level, unless you can figure it out. You went through the tutorial and have a working game, so that’s a good start. After you work with arrays more, you will know what to do, but just deleting a couple cards and thinking the game will still work, there is code tied to the graphics. You have to change the code.
Otherwise, go through the tutorial again and try to understand how they chose the number of cards and how it relates to the game.
So, the simple answer is you have an array called cards. There is also one called sprites. So both of these are probably populated through the inspector, since they are both public. Which means one of those (most likely cards) is the array holding a reference to all your cards in the scene. So, if it had 26 cards for example dragged and dropped into it, and you delete 4 from the scene, you now have 4 empty (null) slots in your array, thus your error as you now have null slots.
This means in the inspector you need to repopulate your array. So you need to find your GameManager script in the scene and fix your arrays. There is a bunch of other stuff I could say about not needing a GameManager per level, but that’s another story.
If this doesn’t make sense to you, you’ll want to start with Unity’s learn section to understand how the inspector works and how to access public variables in it.
okay thank you sir, i will study more of the arrays and i guess i will stick to one level only. hopefully in the future i can be good like you guys. thank u again
Hello Bratmann, just want to ask, is it possible to create 1 GameManager per level? I will not copy and paste it , instead, i will manually do it like what i did when i created the level 3(hard)?Thank you for your response
by the way i was able to fix it! Wooooohooo! What i did was i created each game manager and card scripts for each level. Anyway i know there is another easier way to do that instead of creating each scripts for each level. But im all good. thanks for all your help!