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

Unity is giving NullReferenceException error in my scripts

Discussion in 'General Discussion' started by ahmadalik, Nov 9, 2021.

  1. ahmadalik

    ahmadalik

    Joined:
    Nov 9, 2021
    Posts:
    1
    I am working on a game project in Unity, but for some reason, I get this error:
    Website
    NullReferenceException: Object reference not set to an instance of an object PlayerStone.GetTilesAhead (System.Int32 spacesToMove) (at Assets/Scrpits/PlayerStone.cs:204) PlayerStone.OnMouseUp () (at Assets/Scrpits/PlayerStone.cs:142) UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)
    I have looked through my code many times but I still can not figure out why I get the error because I was not getting it earlier. I checked to make sure nothing was returning null but I still get the error. Here is my code file:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerStone : MonoBehaviour
    {
    // Start is called before the first frame update
    void Start()
    {
    theStateManager = GameObject.FindObjectOfType<StateManager>();

    targetPosition = this.transform.position;
    }

    public Tile StartingTile;
    Tile currentTile;

    public int PlayerId;
    public StoneStorage MyStoneStorage;

    bool scoreMe = false;

    StateManager theStateManager;

    Tile[] moveQueue;
    int moveQueueIndex;

    bool isAnimating = false;

    Vector3 targetPosition;
    Vector3 velocity;
    float smoothTime = 0.25f;
    float smoothTimeVertical = 0.1f;
    float smoothDistance = 0.01f;
    float smoothHeight = 0.5f;

    // Update is called once per frame
    void Update()
    {
    if (!isAnimating)
    {
    return;
    }
    if(Vector3.Distance(
    new Vector3(this.transform.position.x, targetPosition.y, this.transform.position.z),
    targetPosition) < smoothDistance)
    {
    // reached target, check height
    if(moveQueue != null && moveQueueIndex == (moveQueue.Length) && this.transform.position.y > smoothDistance)
    {
    this.transform.position = Vector3.SmoothDamp(
    this.transform.position,
    new Vector3(this.transform.position.x, 0, this.transform.position.z),
    ref velocity,
    smoothTimeVertical);
    }
    else
    {
    AdvanceMoveQueue();
    }
    }


    else if(this.transform.position.y < (smoothHeight - smoothDistance))
    {
    // rise up before moving
    this.transform.position = Vector3.SmoothDamp(
    this.transform.position,
    new Vector3(this.transform.position.x, smoothHeight, this.transform.position.z),
    ref velocity,
    smoothTimeVertical);
    }
    else
    {
    this.transform.position = Vector3.SmoothDamp(
    this.transform.position,
    new Vector3(targetPosition.x, smoothHeight, targetPosition.z),
    ref velocity,
    smoothTime);
    }

    }
    void AdvanceMoveQueue()
    {
    if (moveQueue != null && moveQueueIndex < moveQueue.Length)
    {
    Tile nextTile = moveQueue[moveQueueIndex];
    if (nextTile == null)
    {
    // being scored
    SetNewTargetPosition(this.transform.position + Vector3.right * 100f);
    }
    else
    {
    SetNewTargetPosition(nextTile.transform.position);
    moveQueueIndex++;
    }

    }
    else
    {
    Debug.Log("Done Animating");
    this.isAnimating = false;
    theStateManager.IsDoneAnimating = true;
    }
    }

    void SetNewTargetPosition( Vector3 pos)
    {
    targetPosition = pos;
    velocity = Vector3.zero;
    }

    void OnMouseUp()
    {

    if(theStateManager.CurrentPlayerId != PlayerId)
    {
    return;
    }

    if (!theStateManager.IsDoneRolling)
    {
    // Not time to move
    return;
    }
    if (theStateManager.IsDoneClicking)
    {
    // already moved
    return;
    }

    int spacesToMove = theStateManager.DiceTotal;

    if(spacesToMove == 0)
    {
    return;
    }

    // Where should we end up?

    moveQueue = GetTilesAhead(spacesToMove);
    Tile finalTile = moveQueue[moveQueue.Length - 1];

    if(finalTile == null)
    {
    // scoring stone
    scoreMe = true;
    }
    else
    {
    if(CanLegallyMoveTo(finalTile) == false)
    {
    // not alowed
    finalTile = currentTile;
    moveQueue = null;
    return;
    }
    if(finalTile.PlayerStone != null)
    {
    finalTile.PlayerStone.ReturnToStorage();
    }

    }

    this.transform.SetParent(null); // become batman

    if (currentTile != null)
    {
    currentTile.PlayerStone = null;
    }

    finalTile.PlayerStone = this;

    moveQueueIndex = 0;

    currentTile = finalTile;
    theStateManager.IsDoneClicking = true;
    this.isAnimating = true;
    //
    }
    // Return the list of tile __ moves ahead of us
    Tile[] GetTilesAhead(int spacesToMove)
    {
    if (spacesToMove == 0)
    {
    return null;
    }

    // Where should we end up?

    Tile[] listOfTiles = new Tile[spacesToMove];
    Tile finalTile = currentTile;

    for (int i = 0; i < spacesToMove; i++)
    {
    if (finalTile == null && scoreMe == false)
    {
    scoreMe = true;
    finalTile = StartingTile;
    }
    else
    {
    if (finalTile.NextTiles == null || finalTile.NextTiles.Length == 0)
    {
    // Going of board
    finalTile = null;
    }
    else if (finalTile.NextTiles.Length > 1)
    {
    finalTile = finalTile.NextTiles[PlayerId];
    }
    else
    {
    finalTile = finalTile.NextTiles[0];
    }
    }
    listOfTiles = finalTile;
    }
    return listOfTiles;
    }

    Tile GetTileAhead(int spacesToMove)
    {
    Tile[] tiles = GetTilesAhead(spacesToMove);
    if(tiles == null)
    {
    return currentTile;
    }
    return tiles[tiles.Length-1];
    }

    public bool CanLegallyMoveAhead(int spacesToMove)
    {
    Tile theTile = GetTileAhead(spacesToMove);

    return CanLegallyMoveTo(theTile);
    }

    bool CanLegallyMoveTo(Tile destinationTile)
    {
    if (destinationTile == null)
    {
    Debug.Log("move of board and score");
    return true;
    }

    if(destinationTile.PlayerStone == null)
    {
    return true;
    }

    if(destinationTile.PlayerStone.PlayerId == this.PlayerId)
    {
    // cant land on own stone
    return false;
    }

    // can legaly kick of enemy

    return true;
    }

    public void ReturnToStorage()
    {
    currentTile.PlayerStone = null;
    currentTile = null;

    Vector3 savePosition = this.transform.position;
    MyStoneStorage.AddStoneToStorage(this.gameObject);
    SetNewTargetPosition(this.transform.position);
    this.transform.position = savePosition;
    }

    }
    If you have a solution please let me know so I can fix it
     
  2. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,850
    Format the code using a tag. This is the wrong forum for scripting support. The null ref means you have not referenced the object in question..usually by not drag and dropping it into an Inspector slot.
     
    JoNax97 likes this.
  3. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,554
    Use code tags and use scripting section.

    At a glance, this part:
    Code (csharp):
    1.  
    2. moveQueue[moveQueue.Length - 1];
    3.  
    Likely going to cause a crash, as by default your moveQueue should be null and you don't seem to ever initialize it.
    Code (csharp):
    1.  
    2. Tile[] moveQueue;
    3.  
    Also, learn to use debuggers.