Hey there!
I’ve started working on my very first project with a friend and things have been going swimmingly! We have encountered one problem that has caused us quite and issue, though.
(Second Error) Imgur: The magic of the Internet
In the scripts below, I call “UpdatePath” every time a node changes (i.e. a tower is placed or destroyed on the grid of nodes) so that the units will recalculate their path and go around the towers. The bit of code at the bottom of the PathRequestManager script is the last solution I found to updating every unit’s path. This worked perfectly, however it seems that if a unit dies while this is happening I get a ‘Reference is null’. I also get an error if I run “UpdatePath” when a unit is within 1 node from the end of the path. This gives me an 'Array Index is out of Range" error.
I have tried putting in if(unit == null) return; checks everywhere, I have tried to create a static list of active units to update and have units remove when they die. I just cannot seem to shake these 2 errors. Please. Any help you can provide will truly be appreciated. Thank you!
First Error:
IndexOutOfRangeException: Array index is out of range.
Unit+c__Iterator0.MoveNext () (at Assets/Scripts/Unit.cs:51)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
UnityEngine.MonoBehaviour:StartCoroutine(String)
Unit:OnPathFound(Vector3[ ], Boolean) (at Assets/Scripts/Unit.cs:43)
PathRequestManager:FinishedProcessingPath(Vector3[ ], Boolean) (at Assets/Scripts/PathRequestManager.cs:37)
c__Iterator0:MoveNext() (at Assets/Scripts/Pathfinding.cs:80)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
Second Error:
MissingReferenceException: The object of type ‘Unit’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
Unit.OnPathFound (UnityEngine.Vector3[ ] newPath, Boolean pathSuccessful) (at Assets/Scripts/Unit.cs:42)
PathRequestManager.FinishedProcessingPath (UnityEngine.Vector3[ ] path, Boolean success) (at Assets/Scripts/PathRequestManager.cs:37)
Pathfinding+c__Iterator0.MoveNext () (at Assets/Scripts/Pathfinding.cs:80)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)