Search Unity

Question NullReferenceException

Discussion in 'Scripting' started by slywolf101, Nov 8, 2020.

  1. slywolf101

    slywolf101

    Joined:
    Oct 23, 2020
    Posts:
    9
    Hi I'm very new in writing codes for games and I'm following along with a CodeMonkey YouTube video for making a Minesweeper game and I believe most of my code is working. When saving the console says there is no error present but once I start the game, instead of displaying the minesweeper grid it displays pretty much nothing and produces the error code of "NullReferenceException: Object reference not set to an instance of an object GridPrefabVisual.SetupVisualNode (UnityEngine.Transform visualNodeTransform, MapGridObject mapGridObject) (at Assets/Minesweeper/Scripts/GridPrefabVisual.cs:134)" with a few more examples of an object not being called within GridPrefabVisual. I've been stuck on this error for days now trying to figure it out myself by reading unity forms, watching YouTube video purely dedicated to fixing nullReference, and even downloading the whole game project from CodeMonkey to compare my code with there working one. Through my research I believe I understand that with that error is mostly due because I'm calling something that's not establish within that class and the simple fix is to just declare the object before calling it (I hope) . But my question why when my code that is almost identical to the CodeMonkey's produces an Null error compared to theirs that displays the grid perfectly without clearly establishing the object and what are the different ways I can edit my code so the grid displays. I'll attach my GridPrefabVisual script along with CodeMonkey's version. I can also attach any other files if needed but I don't know the exact scripts that could be helpful for solving my problem.
     

    Attached Files:

  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    visualNodeTransform.Find("hiddenSprite");
    is retuning null, which means there is no object called "hiddenSprite" as a child of your prefab.

    So really it's not a problem with the code, it's a problem with how you set up your prefab. Or at least, the prefab is not set up in the way the code is expecting it to be.
     
    slywolf101 likes this.
  3. slywolf101

    slywolf101

    Joined:
    Oct 23, 2020
    Posts:
    9
    Perefect Thank you soo much this fixed the error and now the grid is sorta displaying correctly and the error is gone. some how while messing with all my prefabs I deleted the hiddenspite one by accident. Now I have some fine tuning to fix my graph because right now its displaying way larger than expected which I believe is preventing the prefab from displaying correcting to its set size. Hopefully I can figure it out but thank you sooo much again!!!
     
    PraetorBlue likes this.