2D Rougelike Tutorial-Part 11 Player Does Not Move
I'm on part 11 of the tutorial. For some reason, the player only moves once. The enemies move, but the player moves only once. The console is saying "NullReferenceException: Object reference not set to an instance of an object Player.Update () (at Assets/My Scripts/Player.cs:24)" and "NullReferenceException: Object reference not set to an instance of an object Player.OnDisable () (at Assets/My Scripts/Player.cs:37)"
Edit: I still did not find the answer. However, when I tested the game, I saw the game manager clone. I looked at the game manager clone in the inspector and saw the A_Game_Manager script attached to it. However, when I moved the character, for some reason the script became disabled. What is happening?
Edit Two: I finished part 12 of the tutorial. I saw that food kept subtracting. However, my player will still not move :(
Game Manager: [code=CSharp]using UnityEngine; using System.Collections; using System.Collections.Generic;
public class A_Game_Manager : MonoBehaviour { public static A_Game_Manager instance = null; public float turnDelay = 0.1f; private BoardManager boardScript; private int level = 3; public int playerFoodPoints = 100; [HideInInspector] public bool playerTurns = true;
private List<Enemy> enemies;
private bool enemiesMoving;
void Awake()
{
if (instance == null)
instance = this;
else if (instance != this)
DestroyObject(gameObject);
enemies = new List<Enemy>();
DontDestroyOnLoad(gameObject);
boardScript = GetComponent<BoardManager>();
InitGame();
}
void InitGame()
{
enemies.Clear();
boardScript.SetUp(level);
}
public void GameOver()
{
enabled = false;
}
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);
}
playerTurns = true;
enemiesMoving = false;
}
void Update()
{
if (playerTurns || enemiesMoving)
return;
StartCoroutine(MoveEnemies());
}
public void AddEnemy(Enemy script)
{
enemies.Add(script);
}
}[/code]
Player [code=CSharp]using UnityEngine; using System.Collections; using System; using UnityEngine.SceneManagement;
public class Player : MovingObject { public int wallDamage = 1; public int pointsPerFood = 20; public int pointsPerSoda = 10; public float LevelDelay = 1f;
private Animator animator;
private int food;
// Use this for initialization
protected override void Start () {
animator = GetComponent<Animator>();
food = A_Game_Manager.instance.playerFoodPoints;
base.Start();
}
void Update()
{
if (!A_Game_Manager.instance.playerTurns) 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);
}
private void OnDisable()
{
A_Game_Manager.instance.playerFoodPoints = food;
}
protected override void AttemptMove<T>(int xDir, int yDir)
{
food -= 10;
base.AttemptMove<T>(xDir, yDir);
CheckIfGameOver();
A_Game_Manager.instance.playerTurns = false;
}
private void CheckIfGameOver()
{
if (food <= 0)
A_Game_Manager.instance.GameOver();
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Exit"))
{
Invoke("Restart", LevelDelay);
enabled = false;
}
if (other.CompareTag("Food"))
{
food += pointsPerFood;
Destroy(other.gameObject);
}
else if (other.CompareTag("Soda"))
{
food += pointsPerSoda;
Destroy(other.gameObject);
}
}
// Update is called once per frame
protected override void OnCantMove<T>(T component)
{
Wall hitWall = component as Wall;
hitWall.DamageWall(wallDamage);
animator.SetTrigger("PlayerChop");
}
private void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
public void LoseFood(int loss)
{
animator.SetTrigger("PlayerHit");
food -= loss;
CheckIfGameOver();
}
}[/code]
Enemy [CODE] using UnityEngine; using System.Collections; using System;
public class Enemy : MovingObject {
public int playerDamage;
private Animator animator;
private Transform target;
private bool skipMove;
protected override void Start()
{
A_Game_Manager.instance.AddEnemy(this);
animator = GetComponent<Animator>();
target = GameObject.FindGameObjectWithTag("Player").transform;
base.Start();
}
protected override void AttemptMove<T>(int xDir, int yDir)
{
if (skipMove)
{
skipMove = false;
return;
}
base.AttemptMove<T>(xDir, yDir);
skipMove = true;
}
public void MoveEnemy()
{
int xDir = 0;
int yDir = 0;
if (Mathf.Abs(target.position.x - transform.position.x) < float.Epsilon)
yDir = target.position.y > transform.position.y ? 1 : -1;
else
xDir = target.position.x > transform.position.x ? 1 : -1;
AttemptMove<Player>(xDir, yDir);
}
protected override void OnCantMove<T>(T component)
{
Player hitPlayer = component as Player;
animator.SetTrigger("enemyAttack");
hitPlayer.LoseFood(playerDamage);
}
}[/CODE]
make sure class A_Game_$$anonymous$$anager is attached to only one game object .
I did make sure that A_Game_$$anonymous$$anager is attached to only one game object.
hi i have cheeked the tutorial it look you have done scripting the same so it might be something in unity because i don't know how enemy can move if game manager script is disabled because both enemy & player inherit from it .
Your answer
Follow this Question
Related Questions
2D Rougelike Tutorial Part 10-Player Does Not Move 1 Answer
RogueLike 2D Tutorial Help 0 Answers
How to make a Inventory Hotbar 0 Answers
end game when all characters dead 0 Answers
2d Rogue like spawn issue 1 Answer