Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by sith4life · Mar 26, 2016 at 07:29 PM · onmousedownnull reference exception

Null Refernce Exception, OnMouseDown

Hi,

I'm getting the ever-so-common null reference exception error. Although, I can't for the life of me, figure out why. The error is as follows:

NullReferenceException: Object reference not set to an instance of an object Tile.OnMouseDown () (at Assets/Scripts/Tile.cs:32) UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)

Tile.cs is:

  public class Tile : MonoBehaviour 
  {
   
 public Vector2 GridPosition = Vector2.zero;

 private Renderer rend;
 private Color defaultColor;
     
 // Use this for initialization
 void Start () {
     rend = GetComponent<Renderer>();
     defaultColor = rend.material.color;
 }
 
 // Update is called once per frame
 void Update () {
 
 }

 void OnMouseEnter()
 {
     rend.material.color = Color.red;
 }
 void OnMouseExit()
 {
     rend.material.color = defaultColor;
 }
 void OnMouseDown()
 {
     Debug.Log("Executes!");
     Debug.Log(this);
     GameManager.Instance.moveCurrentPlayer(this);
     Debug.Log("Maybe?");
 }
 }

and GameManager.cs:

 public class GameManager : MonoBehaviour {
 
     public static GameManager Instance;
     public GameObject TilePrefab;
     public GameObject CharacterPrefab;
 
     public int MapSize = 11;
     int PlayerIndex;
 
     List<List<Tile>> map = new List<List<Tile>>();
     List<Player> players = new List<Player>();
 
     void awake()
     {
         Instance = this;
     }
 
     // Use this for initialization
     void Start ()
     {
         GenerateMap();
         GeneratePlayers();
     }
     
     // Update is called once per frame
     void Update ()
     {
         players[PlayerIndex].TurnUpdate();
 
     }
     public void NextTurn()
     {
         if (PlayerIndex + 1 < players.Count)
         {
             PlayerIndex++;
         }
         else
         {
             PlayerIndex = 0;
         }
     }
     public void moveCurrentPlayer(Tile destinationTile)
     {
     Debug.Log(destinationTile);
     Debug.Log("Player Index: " + PlayerIndex + " : " + players[PlayerIndex]);
     players[PlayerIndex].moveDestination = destinationTile.transform.position + 1.5f * Vector3.up;
     }
 }

Background:

  1. Tile.cs is a component of a "tile" prefab.

  2. These tiles are generated at run time (this works as intended)

  3. The Mouse Enter/Exit stuff changes the colour of the tiles as intended

  4. Debug.Log("Executes") runs, than the null reference exception.

  5. all of the debugs in moveCurrentPlayer Do Not run, nor does Debug.Log("Maybe?");

When I click on the selected tile it gives me above stated error. But, if I debug.log(this) it gives me the Object Name. Additionally, I checked to make sure Players[PlayerIndex] wasn't the issue by commenting it out adding Debug.Log(destinationTile.transform.position) and got the same error. If anyone can help I would be eternally grateful!

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
1
Best Answer

Answer by Paul-Jan · Mar 26, 2016 at 09:08 PM

Awake should be written with a Capital A. The current awake is never called, meaning GameManager.Instance is never initialized, and always remains null.

This will result in the reported Null Reference Exception on the line 37 of Tile.Cs

 GameManager.Instance.moveCurrentPlayer(this);
Comment
Add comment · Show 1 · 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 sith4life · Mar 26, 2016 at 09:10 PM 0
Share

that fixed it. THAN$$anonymous$$S!!

avatar image
1

Answer by Downstream · Mar 26, 2016 at 07:56 PM

@sith4life When dealing with a problem such as this where the null reference is elusive just log everything out one by one. Players, playerIndex and destinationTile. The only thing that can be null in your debug log statement is destinationTile, imo.

There's not enough information to guess what else might be behind it.

Comment
Add comment · Show 1 · 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 sith4life · Mar 26, 2016 at 08:54 PM 0
Share

@downstream thanks for your reply! I put the full scripts in for added visibility a reworded the question to include your suggestion with the debugs.

It looks to me like "this" is null in the Tile.cs script that's being passed to the moveCurrentPlayer method. AFAI$$anonymous$$ this cannot be null... AND the debug statement in the line above shows tile(Clone) (Tile)

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

38 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

Related Questions

Passing OnMouseDown to other Colliders 2 Answers

OnMouseDown() intended behaviour 1 Answer

Simple Camera Movement?! (javascript) 1 Answer

Each consequtive mouse click removes a different Gui.Texture? 1 Answer

Having player object always snap to mouse position 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