Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

The image is not showing correctly NullReferenceException: Object reference not set to an instance o

Discussion in 'Scripting' started by Phenom316, Sep 13, 2017.

  1. Phenom316

    Phenom316

    Joined:
    Sep 13, 2017
    Posts:
    6
    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

    http://www.mediafire.com/file/797u2wtt6uvhmh6/Memory_Game_-_TEST.rar

    Thank you.
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    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.
     
    Last edited: Sep 13, 2017
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    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.
     
  4. Phenom316

    Phenom316

    Joined:
    Sep 13, 2017
    Posts:
    6

    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. :)
     
  5. Phenom316

    Phenom316

    Joined:
    Sep 13, 2017
    Posts:
    6

    Hello, thank you for your reply, here is the code inside this project


    GameManager

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.SceneManagement;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6.  
    7. public class GameManagerLevel3 : MonoBehaviour {
    8.  
    9.     public Sprite[] cardFace;
    10.     public Sprite cardBack;
    11.     public GameObject[] cards;
    12.     public Text matchText;
    13.  
    14.     private bool _init = false;
    15.     private int _matches = 13;
    16.  
    17.     // Update is called once per frame
    18.     void Update() {
    19.         if (!_init)
    20.             initializeCards();
    21.  
    22.         if (Input.GetMouseButtonUp(0))
    23.             checkCards();
    24.     }
    25.  
    26.     void initializeCards() {
    27.         for (int id = 0; id < 2; id++) {
    28.             for (int i = 1; i < 14; i++) {
    29.  
    30.                 bool test = false;
    31.                 int choice = 0;
    32.                 while (!test) {
    33.                     choice = Random.Range(0, cards.Length);
    34.                     test = !(cards[choice].GetComponent<Card>().initialized);
    35.                 }
    36.                 cards[choice].GetComponent<Card>().cardValue = i;
    37.                 cards[choice].GetComponent<Card>().initialized = true;
    38.             }
    39.         }
    40.  
    41.         foreach (GameObject c in cards)
    42.             c.GetComponent<Card>().setupGraphics();
    43.  
    44.         if (!_init)
    45.             _init = true;
    46.     }
    47.  
    48.     public Sprite getCardBack(){
    49.         return cardBack;
    50.     }
    51.  
    52.     public Sprite getCardFace(int i)
    53.     {
    54.         return cardFace[i - 1];
    55.     }
    56.  
    57.     void checkCards()
    58.     {
    59.         List<int> c = new List<int>();
    60.  
    61.         for (int i = 0; i < cards.Length; i++)
    62.         {
    63.             if (cards[i].GetComponent<Card>().state == 1)
    64.                 c.Add(i);
    65.         }
    66.  
    67.         if (c.Count == 2)
    68.             cardComparison(c);
    69.     }
    70.  
    71.     void cardComparison(List<int> c)
    72.     {
    73.         Card.DO_NOT = true;
    74.  
    75.         int x = 0;
    76.         if (cards[c[0]].GetComponent<Card>().cardValue == cards[c[1]].GetComponent<Card>().cardValue) {
    77.             x = 2;
    78.             _matches--;
    79.             matchText.text = "Number of Matches: " + _matches;
    80.             if (_matches == 0)
    81.                 SceneManager.LoadScene("Menu");
    82.         }
    83.  
    84.         for (int i = 0; i < c.Count; i++)
    85.         {
    86.             cards[c[i]].GetComponent<Card>().state = x;
    87.             cards[c[i]].GetComponent<Card>().falseCheck();
    88.         }
    89.     }
    90. }
    91.  
    Card.cs

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class Card : MonoBehaviour
    6. {
    7.  
    8.     public static bool DO_NOT = false;
    9.  
    10.     [SerializeField]
    11.     private int _state;
    12.     [SerializeField]
    13.     private int _cardValue;
    14.     [SerializeField]
    15.     private bool _initialized = false;
    16.  
    17.     private Sprite _cardBack;
    18.     private Sprite _cardFace;
    19.  
    20.     private GameObject _manager;
    21.  
    22.     void Start()
    23.     {
    24.         _state = 1;
    25.         _manager = GameObject.FindGameObjectWithTag("Manager");
    26.     }
    27.  
    28.     public void setupGraphics()
    29.     {
    30.         _cardBack = _manager.GetComponent<GameManagerLevel3>().getCardBack();
    31.         _cardFace = _manager.GetComponent<GameManagerLevel3>().getCardFace(_cardValue);
    32.  
    33.         flipCard();
    34.     }
    35.  
    36.     public void flipCard() {
    37.  
    38.         if (_state == 0)
    39.             _state = 1;
    40.         else if (_state == 1)
    41.             _state = 0;
    42.  
    43.         if (_state == 0 && !DO_NOT)
    44.             GetComponent<Image>().sprite = _cardBack;
    45.         else if (_state == 1 && !DO_NOT)
    46.             GetComponent<Image>().sprite = _cardFace;
    47.     }
    48.  
    49.     public int cardValue
    50.     {
    51.         get { return _cardValue; }
    52.         set { _cardValue = value; }
    53.     }
    54.  
    55.     public int state
    56.     {
    57.         get { return _state; }
    58.         set { _state = value; }
    59.     }
    60.  
    61.     public bool initialized
    62.     {
    63.         get { return _initialized; }
    64.         set { _initialized = value; }
    65.     }
    66.  
    67.     public void falseCheck()
    68.     {
    69.         StartCoroutine(pause());
    70.     }
    71.  
    72.     IEnumerator pause()
    73.     {
    74.         yield return new WaitForSeconds(1);
    75.         if (_state == 0)
    76.             GetComponent<Image>().sprite = _cardBack;
    77.         else if (_state == 1)
    78.             GetComponent<Image>().sprite = _cardFace;
    79.         DO_NOT = false;
    80.     }
    81. }

    Here is the tutorial of the video in youtube.



    credits to Hunter Music And TV


    Hope anyone can help me please? please also see image attachment Capture.JPG
     
  6. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    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.
     
    Last edited: Sep 13, 2017
    Phenom316 likes this.
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    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.
     
    Last edited: Sep 13, 2017
    Phenom316 likes this.
  8. Phenom316

    Phenom316

    Joined:
    Sep 13, 2017
    Posts:
    6

    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 :D
     
  9. Phenom316

    Phenom316

    Joined:
    Sep 13, 2017
    Posts:
    6

    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
     
  10. Phenom316

    Phenom316

    Joined:
    Sep 13, 2017
    Posts:
    6
    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!