I need some helps

Updated on January 24, 2018 in [A] C# .Net
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
6 on January 20, 2018

So um… i am very new to Unity and i am working on a game of mine, I have encountered a problem of :

 

“NullReferenceException: Object reference not set to an instance of an object
Square.SetEmpty ()”.

 

Here is the code my Square :

public class Square : MonoBehaviour
{
  public int indRow;
  public int indCol;
  public int PieceTextureNumber
   {
    get
     {
       return number;
     } 
    set
    {
      number = value;
      if (number == 0)
         SetEmpty();
     else
       {
         ApplyStyleFromHolder(number);
        SetVisible();
       }
     }
  }
  private int number;
  private Canvas SquareCanvas;
  private RawImage PieceTexture;
  private void Awake()
   {
    PieceTexture = GetComponentInChildren<RawImage>();
    SquareCanvas = GetComponentInChildren<Canvas>();
   }
  void ApplyStyleFromHolder(int index)
   {
    PieceTexture.texture = SquarePieceHolder.Instance.SquareStyles[index].NewTexture;
   }
  private void SetVisible()
   {
    SquareCanvas.enabled = true;
    PieceTexture.enabled = true;
   }
  private void SetEmpty()
   {  
    SquareCanvas.enabled = false;
    PieceTexture.enabled = false;
   }
}

 If any of you know how to fix then please help me, and thank you for taking a look at this post atleast

  • Liked by
Reply
5 on January 20, 2018

You’re never defining where the reference to the canvas is.

on January 21, 2018

Sorry, i dont understand “reference to the canvas”

on January 21, 2018

The reference to the Canvas should be shown as an empty slot, by your script, in the Inspector window, when a GameObject with that script is highlighted.

If you drag and drop the Canvas GameObject into the slot, it should now be referenced in your script.

on January 21, 2018

Into what slot? (sorry i am very new to this)

I dont understand the “as an empty slot, by your script,  in the Inspector window, when a GameObject with that script is highlighted.”

 

Devoted
on January 23, 2018

This is a really difficult question to try to help with… Terms that Becqurel is using should be learned pretty soon. I’ll try to break it down a little bit for you:

  • Here is an “Editor Breakdown” to show the various parts of the default view
  • “Empty slots” are found in the inspector window where your variable on your script is.
  • You can assign a compatible object (of the same type) from your Project window to the Empty slot in order to assign it. This creates a reference to the object you want applied, when the script starts in-game.

And don’t worry. All this will come to you through practice. The best thing you can do for now though is provide details and screenshots in order to create the ‘best’ (easier to answer) questions.

 

on January 23, 2018

Hey BinaryBeal can we take on discord? This is my name Sir. K#7673

 

Show more replies
  • Liked by
Reply
Cancel