Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 Andy-Van · May 16, 2016 at 06:05 PM · 2d

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.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 4 · Share
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 Andy-Van · May 16, 2016 at 07:01 PM 0
Share

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...

alt text

I appreciate the suggestions though, thank you :)

unityscreengrab.jpg (320.9 kB)
avatar image Andy-Van · May 16, 2016 at 07:08 PM 0
Share

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!

avatar image Jessespike · May 16, 2016 at 07:12 PM 0
Share

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.

avatar image Andy-Van · May 16, 2016 at 11:30 PM 0
Share

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 :) alt text

unityscreengrab.jpg (258.2 kB)
avatar image
0

Answer by sakuralittlered · Oct 07, 2017 at 08:53 AM

So how did you fix this bug? Please teach me! Thank you so much

Comment
Add comment · Share
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

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.

Follow this Question

Answers Answers and Comments

81 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Doodle Jump Game Platform Spawn Issue Unity2D 0 Answers

Keep Top Down 2D camera in bounds of background sprite 3 Answers

Why do I keep getting a null reference? Its not null! 0 Answers

How can i make pause menu 2 Answers

Checking if the player jumps while not grounded 0 Answers


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