Player.cs erroring out for Roguelike tutorial in Unity v5.4
SO I am trying to work along with the roguelike tutorial using 5.4v of Unity. I have worked all the way through to 4.01 Adding UI & Level transitions. When i went to text the game to see if the new code worked i realized the player character doesn't seem to work I get 3 errors with the third error happening every frame,
1st - NullReferenceException: Object reference not set to an instance of an object Player.Start () (at Assets/Scripts/Player.cs:21) Line 21 being - Calories = GameManager.instance.playerCalories;
2nd - NullReferenceException: Object reference not set to an instance of an object Player.OnDisable () (at Assets/Scripts/Player.cs:30) Line 35 being - GameManager.instance.playerCalories = Calories;
3rd - NullReferenceException: Object reference not set to an instance of an object Player.Update () (at Assets/Scripts/Player.cs:35) Line 35 being - if (!GameManager.instance.playersTurn)
Player.cs***
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Player : MovingObject
{
public int wallDamage = 1;
public int pointsPerFood = 10;
public int pointsPerSoda = 20;
public float restartLevelDelay = 1f;
public Text caloriesText;
Animator animator;
int Calories;
protected override void Start ()
{
animator = GetComponent<Animator> ();
Calories = GameManager.instance.playerCalories;
caloriesText.text = "Calories: " + Calories;
base.Start();
}
private void OnDisable()
{
GameManager.instance.playerCalories = Calories;
}
private void Update ()
{
if (!GameManager.instance.playersTurn)
return;
int horizontal = 0;
int vertical = 0;
horizontal = (int) (Input.GetAxisRaw ("Horizontal"));
vertical = (int) (Input.GetAxisRaw ("Vertical"));
if (horizontal != 0)
{
vertical = 0;
}
if (horizontal != 0 || vertical != 0)
{
AttemptMove<Wall> (horizontal, vertical);
}
}
protected override void AttemptMove <T> (int xDir, int yDir)
{
Calories--;
caloriesText.text = "Calories: " + Calories;
base.AttemptMove <T> (xDir, yDir);
RaycastHit2D hit;
if (Move (xDir, yDir, out hit)) {
}
CheckIfGameOver();
GameManager.instance.playersTurn = false;
}
private void OnTriggerEnter2D (Collider2D other)
{
if (other.tag == "Exit") {
Invoke ("Restart", restartLevelDelay);
enabled = false;
}
else if (other.tag == "Food")
{
Calories += pointsPerFood;
caloriesText.text = "+" + pointsPerFood + " Calories: " + Calories;
other.gameObject.SetActive (false);
}
else if (other.tag == "Soda")
{
Calories += pointsPerSoda;
caloriesText.text = "+" + pointsPerSoda + " Calories: " + Calories;
other.gameObject.SetActive (false);
}
}
protected override void OnCantMove <T> (T component)
{
Wall hitWall = component as Wall;
hitWall.DamageWall (wallDamage);
animator.SetTrigger ("playerChop");
}
private void Restart()
{
SceneManager.LoadScene("Main");
}
public void LoseCalories (int loss)
{
animator.SetTrigger("playerHit");
Calories -= loss;
caloriesText.text = "-" + loss + " Calories: " + Calories;
CheckIfGameOver();
}
private void CheckIfGameOver()
{
if (Calories <= 0)
GameManager.instance.GameOver ();
}
}
GameManager.cs****
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour {
public float levelStartDelay = 1f;
public float turnDelay = .1f;
public static GameManager instance = null;
public int playerCalories= 100;
[HideInInspector] public bool playersTurn = true;
Text levelText;
GameObject levelImage;
BoardManager boardScript;
int level = 1;
List<Enemy> enemies;
bool enemiesMoving;
bool doingSetup = true;
void Awake ()
{
if (instance = null)
instance = this;
else if (instance != this)
Destroy (gameObject);
DontDestroyOnLoad (gameObject);
enemies = new List<Enemy> ();
boardScript = GetComponent<BoardManager> ();
InitGame ();
}
private void onSceneLoaded (int index)
{
level++;
InitGame ();
}
void InitGame()
{
doingSetup = true;
levelImage = GameObject.Find ("LevelImage");
levelText = GameObject.Find ("LevelText").GetComponent<Text> ();
levelText.text = "DAY " + level;
levelImage.SetActive (true);
Invoke ("HideLevelImage", levelStartDelay);
enemies.Clear ();
boardScript.SetupScene (level);
}
private void HideLevelImage()
{
levelImage.SetActive (false);
doingSetup = false;
}
public void GameOver ()
{
levelText.text = "After " + level + " days, you've starved to death.";
levelImage.SetActive(true);
enabled = false;
}
void Update () {
if (playersTurn || enemiesMoving || doingSetup)
return;
StartCoroutine (MoveEnemies ());
}
public void AddEnemyToList(Enemy script)
{
enemies.Add (script);
}
IEnumerator MoveEnemies()
{
enemiesMoving = true;
yield return new WaitForSeconds(turnDelay);
if (enemies.Count == 0) {
yield return new WaitForSeconds(turnDelay);
}
for (int i = 0; i < enemies.Count; i++) {
enemies [i].MoveEnemy ();
yield return new WaitForSeconds (enemies [i].moveTime);
}
playersTurn = true;
enemiesMoving = false;
}
}
MovingObect.cs**
using UnityEngine;
using System.Collections;
public abstract class MovingObject : MonoBehaviour
{
public float moveTime = 0.1f;
public LayerMask blockingLayer;
BoxCollider2D boxCollider;
Rigidbody2D rb2D;
float inverseMoveTime;
protected virtual void Start ()
{
boxCollider = GetComponent<BoxCollider2D> ();
rb2D = GetComponent<Rigidbody2D> ();
inverseMoveTime = 1f / moveTime;
}
protected bool Move (int xDir, int yDir, out RaycastHit2D hit)
{
Vector2 start = transform.position;
Vector2 end = start + new Vector2 (xDir, yDir);
boxCollider.enabled = false;
hit = Physics2D.Linecast (start, end, blockingLayer);
boxCollider.enabled = true;
if (hit.transform == null)
{
StartCoroutine (SmoothMovement (end));
return true;
}
return false;
}
protected IEnumerator SmoothMovement (Vector3 end)
{
float sqrRemainingDistance = (transform.position - end).sqrMagnitude;
while (sqrRemainingDistance > float.Epsilon)
{
Vector3 newPosition = Vector3.MoveTowards (rb2D.position, end, inverseMoveTime * Time.deltaTime);
rb2D.MovePosition (newPosition);
sqrRemainingDistance = (transform.position - end).sqrMagnitude;
yield return null;
}
}
protected virtual void AttemptMove <T> (int xDir, int yDir)
where T : Component
{
RaycastHit2D hit;
bool canMove = Move (xDir, yDir, out hit);
if (hit.transform == null)
return;
T hitComponent = hit.transform.GetComponent<T>();
if(!canMove && hitComponent != null)
OnCantMove(hitComponent);
}
protected abstract void OnCantMove <T> (T component)
where T : Component;
}
The solution may be obvious to most of you but I am a noob, so I guess if this error just makes you laugh at me maybe there are other tutorials you could point me in the direction of that would help me flesh out my understanding of how C# works with Unity. either way thank you all for whatever time you waste on this with me.
Answer by Jessespike · May 16, 2016 at 06:55 PM
Looks like GameManager instance isn't being assigned. This can happen if GameManager is not on a GameObject in the scene, because instance gets set in Awake().
Put GameManager script on a GameObject in the scene.
----edit-------
This is probably a typo:
if (instance = null)
instance = this;
shouldn't it be:
if (instance == null)
instance = this;
"==" is a comparison, "=" is an assignment.
I've got the loader script on the main camera bringing in a empty game object prefab called Game$$anonymous$$anager which has the Game$$anonymous$$anager.cs and Boardmanager.cs on it. Boardmanager works flawlessly and I even tried copy and pasting in the completed script for loader.cs to see if anything would change but the same errors on the player script appear, I was hoping the issue was with the tutorial being old and some section of script being out dated but it seems my scripts just can't recognize each other because of error I made that I can't find, I've been re-watching the video's while looking at my script to find what i miss understood but can't find anything yet...
I appreciate the suggestions though, thank you :)
Thank you , :) I have spent literally hours overlooking that... Thank you so much, everything is working perfectly now, no errors, Thanks A LOT! There are some other errors to work out with the collision detection on pickups and advancing in levels each iteration but I will figure it all out in time, Thanks again!
Player is trying to access Game$$anonymous$$anager before it's even loaded in the scene. I'm not familiar with this rogue tutorial, but logic suggests that Game$$anonymous$$anager needs to exist before Player can access it. You can try disabling the Player and enable it when Game$$anonymous$$anager is loaded. For example, in Game$$anonymous$$anager:Awake():
FindObjectOfType<Player>().enabled = true;
or if the entire player GameObject is disabled:
FindObjectOfType<Player>().gameObject.SetActive(true);
Player needs to be disabled by default.
A side note: Here on Unity Answers, do not submit answers as comments. Hit the reply link if you want to add a comment. Reserve answers for answers, comments for comments.
If anyone is having trouble with collision detection regarding enemy's, pick-ups, exit tiles, I found the players box collider activates every tile he is touching/standing next to, so modifying all the 2D box colliders size to x0.8 and y0.8 stops that from happening so they will only activate once you walk over it. Thanks again jessespike for finding my code error :)
Answer by sakuralittlered · Oct 07, 2017 at 08:53 AM
So how did you fix this bug? Please teach me! Thank you so much
Your answer
Follow this Question
Related Questions
Is It Bad to be Tired of my Own Mobile Game? 1 Answer
Why/How 2d tower of blocks collapse? 0 Answers