• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by greatness · Mar 28, 2016 at 03:22 AM · 2d gameplayergametutorialroguelike

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]

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ali-hatem · Mar 28, 2016 at 11:07 AM 0
Share

make sure class A_Game_$$anonymous$$anager is attached to only one game object .

avatar image greatness Ali-hatem · Mar 28, 2016 at 07:23 PM 0
Share

I did make sure that A_Game_$$anonymous$$anager is attached to only one game object.

avatar image Ali-hatem greatness · Mar 29, 2016 at 08:56 PM 0
Share

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 .

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

72 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make a Inventory Hotbar 0 Answers

2D Rougelike Tutorial Part 10-Player Does Not Move 1 Answer

RogueLike 2D Tutorial Help 0 Answers

2D Rougelike Tutorial-Part 13 -Music Will Not Play 0 Answers

2d Rogue like spawn issue 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges