Creating a Pacman replica. Pacman does not move anywhere on play mode.,Creating Pacman game for a student project. Pacman doesn't move anywhere when in Play mode
Hi! So I am a student and we just started learning Unity. For this project we are creating a replica of a Pacman game. When trying to make pacman move, we are using the neighbour Node technique, where we add Nodes in every intersection across the map, and Pacman can move to the neighbouring intersection.
When I enter Play mode, I get the following errors:
NullReferenceException: Object reference not set to an instance of an object Pacman.CanMove (Vector2 d) (at Assets/Assets/Scripts/Pacman.cs:100) Pacman.MoveToNode (Vector2 d) (at Assets/Assets/Scripts/Pacman.cs:64) Pacman.CheckInput () (at Assets/Assets/Scripts/Pacman.cs:41) Pacman.Update () (at Assets/Assets/Scripts/Pacman.cs:27)
Pacman is currently located on an intersection, so it should be available to detect the current move options and neighbour Nodes around.
Here is the script where the error is located:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Pacman : MonoBehaviour {
public float speed = 4.0f;
private Vector2 direction = Vector2.zero;
private Node currentNode;
// Use this for initialization
void Start () {
Node node = GetNodeAtPosition (transform.localPosition);
if (node != null) {
currentNode = node;
Debug.Log(currentNode);
}
}
// Update is called once per frame
void Update () {
CheckInput();
//Move();
UpdateOrientation();
}
void CheckInput () {
if (Input.GetKeyDown (KeyCode.LeftArrow)) {
direction = Vector2.left;
MoveToNode(direction);
} else if (Input.GetKeyDown(KeyCode.RightArrow)) {
direction = Vector2.right;
MoveToNode(direction);
} else if (Input.GetKeyDown(KeyCode.UpArrow)) {
direction = Vector2.up;
MoveToNode(direction);
} else if (Input.GetKeyDown(KeyCode.DownArrow)) {
direction = Vector2.down;
MoveToNode(direction);
}
}
void Move () {
transform.localPosition += (Vector3)(direction * speed) * Time.deltaTime;
}
void MoveToNode (Vector2 d) {
Node moveToNode = CanMove(d);
if (moveToNode != null) {
transform.localPosition = moveToNode.transform.position;
currentNode = moveToNode;
}
}
void UpdateOrientation () {
if (direction == Vector2.left) {
transform.localScale = new Vector3(-1, 1, 1);
transform.localRotation = Quaternion.Euler(0, 0, 0);
} else if (direction == Vector2.right) {
transform.localScale = new Vector3(1, 1, 1);
transform.localRotation = Quaternion.Euler(0, 0, 0);
}else if (direction == Vector2.up){
transform.localScale = new Vector3(1, 1, 1);
transform.localRotation = Quaternion.Euler(0, 0, 90);
}else if (direction == Vector2.down){
transform.localScale = new Vector3(1, 1, 1);
transform.localRotation = Quaternion.Euler(0, 0, 270);
}
}
// THIS IS THE SECTION WHERE ERROR IS LOCATED ACCORDING TO THE CONSOLE//
Node CanMove (Vector2 d) {
Node moveToNode = null;
for (int i = 0; i < currentNode.neighbors.Length; i++) { //**THIS IS THE LINE WHERE ERROR IS LOCATED ACCORDING TO CONSOLE**//
if (currentNode.validDirections [i] == d) {
moveToNode = currentNode.neighbors [i];
break;
}
}
return moveToNode;
}
// END OF SECTION WHERE ERROR IS LOCATED//
Node GetNodeAtPosition (Vector2 pos) {
GameObject tile = GameObject.Find ("Game").GetComponent<GameBoard> ().board [(int)pos.x, (int)pos.y];
if (tile != null) {
return tile.GetComponent<Node> ();
}
return null;
}
}
Can someone help me with this problem? Thank you so much in advance!
Your answer