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 sinithparanga · Jan 02, 2018 at 12:17 PM · c#nullreferenceexceptionclassunity player

Clicking out and then in to the Unity Player while game is running will something throw me a Null Exception.

Hello,

happy new year to all of you!

I have this problem an the script background_beha. When I leave the Unity Player running while I check on my script in Mono Develop and click back to the player to continue playing, it sometimes throws me a Null Exception. After Debbuging it seems that "Grid" is null inside the Grid Class, while accessing the method NodeFromWorldPoint(). It is pretty weird and I don't have any clue on how to debug this.

When I play the game and don't switch windows or do any other stuff on the computer everything runs fine. The error only appears when I click in and out of the Unity Player Window.

As a background, the Grid Class:MonoBehaviour is a Component of the A* GameObject. In its Start() it Instantiate the Background Sprite (which has the background_beha Script attached) like this:

 void CreateBuild() {
         grid = new Node[gridSizeX, gridSizeY];
         worldBottomLeft = transform.position - Vector3.right * gridWorldSize.x / 2 - Vector3.up * gridWorldSize.y / 2;
 
         for (int x = 0; x < gridSizeX; x++) {
             for (int y = 0; y < gridSizeY; y++) {
                 Vector3 worldPoint = worldBottomLeft + Vector3.right * (x*nodeDiameter+nodeRadius) + Vector3.up *(y*nodeDiameter+nodeRadius);
                 Vector3 snapPoint = worldBottomLeft + Vector3.right * (x*nodeDiameter+nodeDiameter) + Vector3.up *(y*nodeDiameter+nodeDiameter);
 
                 grid [x, y] = new Node (worldPoint, true, x, y,snapPoint,primaryElement);
                 Instantiate (gridTexture.gameObject, grid[x,y].getWorldCoor (), Quaternion.Euler (Vector3.zero),gridParent);
                 Instantiate (backgroundSprite.gameObject, grid[x,y].getWorldCoor (), Quaternion.Euler (Vector3.zero),backgroundParent);
 
             }
         }
         gridParent.gameObject.SetActive (false);
     }

 

Here the NodeFromWorldPoint() inside the Grid Class:

     public Node NodeFromWorldPoint (Vector3 _worldPosition) {
         float percentX = (_worldPosition.x + gridWorldSize.x / 2) / gridWorldSize.x;
         float percentY = (_worldPosition.y + gridWorldSize.y / 2) / gridWorldSize.y;
         percentX = Mathf.Clamp01 (percentX);
         percentY = Mathf.Clamp01 (percentY);
 
         int x = Mathf.RoundToInt((gridSizeX - 1) * percentX);
         int y = Mathf.RoundToInt((gridSizeY - 1) * percentY);
 
         return grid [x, y];
     }
 


And here is the background_beha Class that is part of the backgroundSprite that is instantiated inside the Grid.

 public class background_beha : MonoBehaviour {
 
     public Sprite day,night;
     public GameObject ResText;
     public GameObject CoordText;
     public GameObject WeightPenaltyText;
 
     public GameObject elementIcon;
     public Vector3 localSlotLocation2;
     public Vector3 localSize = new Vector3 (0.4f, 0.4f, 1f);
 
     [SerializeField]
     private int showStatus = 0;
 
     private Grid theGrid;
     private Node currentNode;
     private Transform refObject;
     private float worldSizeX;
     private SpriteRenderer sr;
 
     private Inventory inventory;
 
     private List<GameObject> localSlot2 = new List<GameObject>();
 
     // Use this for initialization
     void Start () {
         ResText.SetActive (false);
         CoordText.SetActive (false);
         WeightPenaltyText.SetActive (false);
 
         Setting_Button_Controller.showRessourceCap += Setting_Button_Controller_showRessourceCap;
 
         refObject = GameObject.Find ("Night0.1Ref").transform;
         sr = GetComponent<SpriteRenderer> ();
 
         theGrid = GameObject.Find ("A*").GetComponent<Grid> ();
         currentNode = theGrid.NodeFromWorldPoint (transform.position);
         worldSizeX = theGrid.gridWorldSize.x;
         inventory = currentNode.inventory;
     }
 
 
 
     void Setting_Button_Controller_showRessourceCap ()
     {
         if (showStatus < 2)
             showStatus++;
         else
             showStatus = 0;
 
         switch (showStatus) {
         case 0:
             ResText.SetActive (false);
             CoordText.SetActive (false);
             WeightPenaltyText.SetActive (false);
             break;
         case 1:
             ResText.SetActive (true);
             CoordText.SetActive (true);
             WeightPenaltyText.SetActive (false);
             break;
         case 2:
             ResText.SetActive (false);
             CoordText.SetActive (true);
             WeightPenaltyText.SetActive (true);
             break;
         }
 
     }
     
     void Update() {
         float dif = Mathf.Abs (refObject.position.x - transform.position.x);
         float alpha = Mathf.Sin (dif * Mathf.PI / worldSizeX);
 
         if (currentNode == null) {
 // HERE THE ERROR APPEARS !! //
             Debug.LogError ("currentNode is null!");
             theGrid = GameObject.Find ("A*").GetComponent<Grid> ();
             currentNode = theGrid.NodeFromWorldPoint (transform.position);
         }
 
         if (alpha >= 0.5) {
             sr.sprite = day;    
             currentNode.isNight = false;
         } else {
             sr.sprite = night;
             currentNode.isNight = true;
         }
 
         ResText.GetComponent<TextMesh> ().text = currentNode.inventory.Count(currentNode.primaryElement).ToString();
         Vector2 gridPos = currentNode.getGridPosition ();
         CoordText.GetComponent<TextMesh> ().text = Mathf.RoundToInt (gridPos.x) + "," + Mathf.RoundToInt (gridPos.y);
         WeightPenaltyText.GetComponent<TextMesh> ().text = currentNode.movementPenalty.ToString ();
     }
 
     void ShowInv() {
         int counter = 0;
         foreach (Element _e in inventory.AllElementsinInventory()) {
             if (_e != currentNode.primaryElement) {
 
                 //print (gameObject.name+ ":found Element in Inventory: " + _e.elementName);
                 GameObject tempSlot = localSlot2.Find (i => i.name == "LocalSlot_" + _e.name);
                 if (tempSlot == null) {
                     tempSlot = (GameObject)Instantiate (elementIcon, transform);
                     tempSlot.transform.localScale = localSize;
                     tempSlot.transform.localPosition = localSlotLocation2 + new Vector3 (localSlotLocation2.x + 2 * counter, localSlotLocation2.y, localSlotLocation2.z);
                     tempSlot.name = "LocalSlot_" + _e.name;
                     tempSlot.GetComponent<ElementBehaivor> ().Element = _e;
                     tempSlot.GetComponent<ElementBehaivor> ().Amount = inventory.Count (_e);
 
                     localSlot2.Add (tempSlot);
                 } else {
                     if (inventory.Count (_e) > 0) {
                         tempSlot.SetActive (true);
                         tempSlot.transform.localPosition = localSlotLocation2 + new Vector3 (localSlotLocation2.x + 2 * counter, localSlotLocation2.y, localSlotLocation2.z);
                         tempSlot.GetComponent<ElementBehaivor> ().Amount = inventory.Count (_e);
                         counter++;
                     } else {
                         tempSlot.SetActive (false);
                     }
                 }
             }
         }
     }
 }



Project Background:

Prototyping Stage, A kind of city builder/strategy game. Doing it in my free time. First time developing a game. 10+ experience developing/designing/implementing business applications. I know the code is ugly.

Thanks for the help on guiding me where to keep digging into this.

Christian

Comment
Add comment · Show 1
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 TreyH · Jan 02, 2018 at 03:00 PM 0
Share

The only thing in that function not declared in the function's scope is gridWorldSize. It's not set anywhere in your code above, where / when are you declaring it?

2 Replies

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

Answer by Glurth · Jan 02, 2018 at 05:47 PM

You mention this only happens sometimes.. is it possible his only occurs when you save a change to code? If the code gets recompiled, while in play-mode in the editor, it will reserialize everything. If the Node class does properly serialize, this would cause references to instances of it, to become null.

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 sinithparanga · Jan 02, 2018 at 06:40 PM 0
Share

If the code gets recompiled, while in play-mode in the editor, it will reserialize everything.

Can that happen? I will try to reproduce...

avatar image sinithparanga sinithparanga · Jan 02, 2018 at 06:45 PM 0
Share

Yes! I could reproduce the error. I changed the code, saved and it recompiled in the background. 2 sec later the error appeared! Wow. Never thought of this, this means could do changes on the fly without restarting the game every time.
Thanks!

avatar image meat5000 ♦ sinithparanga · Jan 03, 2018 at 09:58 AM 0
Share

Dont forget to accept the answer if it was the one!

avatar image Glurth sinithparanga · Jan 02, 2018 at 09:04 PM 0
Share

yeah, really powerful.

avatar image
0

Answer by meat5000 · Jan 02, 2018 at 12:55 PM

Try checking the Run In Background option.

https://docs.unity3d.com/ScriptReference/Application-runInBackground.html

This should keep things ticking over when focus is lost.


As an alternative, you can snapshot the variables that are giving you the trouble and feed them in; sort of using a dummy variable but using the last given information.

There is a method to do this

https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationFocus.html

and a read-only bool if you want to check the status of focus

https://docs.unity3d.com/ScriptReference/Application-isFocused.html


When something will break if not supplied with constant information, add a '[catch][1]', either programming-literal or of your own creation; something as simple as an 'if null' or 'if !object'. Some level of pre-testing in the code will usually prevent these situations. [1]: https://www.google.co.uk/search?q=programming+catch&ie=utf-8&oe=utf-8&client=firefox-b&gfe_rd=cr&dcr=0&ei=ln9LWpWXMaPP8AfA8L6ACg

Comment
Add comment · Show 2 · 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 sinithparanga · Jan 02, 2018 at 01:38 PM 0
Share

Thx for the quick answer. the Option "Run In Background" is turned on. I leave the game running in background while I check the code.

If Null is active, that is the weird thing, look at line 75 of background_beha.

I am implementing a Contructor that assigned the Grid and Node called by the upper Grid Class. $$anonymous$$aybe this will change the behavior or guide me somewhere. Try/Catch will be my next approach. Thx

avatar image meat5000 ♦ sinithparanga · Jan 02, 2018 at 02:42 PM 0
Share

Is it an actual error that is thrown up or just the Debug.Log?

Have two Node variables. At the end of the frame make

 currentNode = lastNode;

If this does not solve the problem you need to Debug Log a bit deeper to see where the loss of information occurs.

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

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

Getting NullReferenceException when creating class instance using List 1 Answer

NullReferenceException yet Debug.Log shows nothing is null? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Return a value from a custom class 3 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