Search Unity

NullReferenceException: Object reference not set to an instance of an object

Discussion in 'Scripting' started by KenNoKage, May 24, 2019.

  1. KenNoKage

    KenNoKage

    Joined:
    Feb 14, 2017
    Posts:
    3
    Hi,

    I'm trying Match3 game. But I can't move characters in any direction. I've compared it to the original script. It doesn't look wrong. I couldn't find the error with Debug.Log. Since I am a beginner I cannot use the commands exactly. I don't speak English. Turkish videos are very bad. Can you help me, please?

    Thanks. Bye.

    Error Result 1:
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. Tile.Select () (at Assets/Scripts/Board and Grid/Tile.cs:26)
    3. Tile.OnMouseDown () (at Assets/Scripts/Board and Grid/Tile.cs:52)
    4. UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)
    Error Result 2:
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. Tile.Select () (at Assets/Scripts/Board and Grid/Tile.cs:26)
    3. Tile.OnMouseDown () (at Assets/Scripts/Board and Grid/Tile.cs:67)
    4. UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)
    ---------------------------------------------------------------------------------------------------------------------

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class Tile : MonoBehaviour
    6. {
    7.     private static Color selectedColor = new Color(.5f, .5f, .5f, 1.0f);
    8.     private static Tile previousSelected = null;
    9.  
    10.     private SpriteRenderer render;
    11.     private bool isSelected = false;
    12.  
    13.     private Vector2[] adjacentDirections = new Vector2[] { Vector2.up, Vector2.down, Vector2.left, Vector2.right };
    14.  
    15.     private bool matchFound = false;
    16.  
    17.     void Awake()
    18.     {
    19.         render = GetComponent<SpriteRenderer>();
    20.     }
    21.     private void Select()
    22.     {
    23.         isSelected = true;
    24.         render.color = selectedColor;
    25.         previousSelected = gameObject.GetComponent<Tile>();
    26.         SFXManager.instance.PlaySFX(Clip.Select);
    27.     }
    28.  
    29.     private void Deselect()
    30.     {
    31.         isSelected = false;
    32.         render.color = Color.white;
    33.         previousSelected = null;
    34.     }
    35.  
    36.     void OnMouseDown()
    37.     {
    38.         if (render.sprite == null || BoardManager.instance.IsShifting)
    39.         {
    40.             return;
    41.         }
    42.  
    43.         if (isSelected) // Is it already selected?
    44.         {
    45.             Deselect();
    46.         }
    47.  
    48.         else
    49.         {
    50.             if (previousSelected == null) // Is it the first tile selected?
    51.             {
    52.                 Select();
    53.             }
    54.  
    55.             else
    56.             {
    57.                 if (GetAllAdjacentTiles().Contains(previousSelected.gameObject))
    58.                 {
    59.                    SwapSprite(previousSelected.render);
    60.                     previousSelected.ClearAllMatches();
    61.                     previousSelected.Deselect();
    62.                     ClearAllMatches();
    63.                 }
    64.                 else
    65.                 {
    66.                     previousSelected.GetComponent<Tile>().Deselect();
    67.                     Select();
    68.                 }
    69.             }
    70.         }
    71.     }
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    734
    Check if:
    • Your SFXManager instance exists.
    • The Clip your SFXManager is trying to play exists.
     
    KenNoKage likes this.
  3. KenNoKage

    KenNoKage

    Joined:
    Feb 14, 2017
    Posts:
    3
    Thanks. I will try. I could not. :(
     
    Last edited: May 24, 2019
  4. KenNoKage

    KenNoKage

    Joined:
    Feb 14, 2017
    Posts:
    3
    I'll erase everything and start over.