Unity 5.1.1 is skipping code lines
I have the following code:
private void scan(GameObject puppet)
{
setRunningAway(false);
_nearbyUnits = puppet.GetComponent<AIPlayerController>().CurrentRegion.UnitsInRegion();
RaycastHit righthit, lefthit, tophit, bottomhit;
int _adjacentRegIdx = 0;
//We'll use an incomplete if-then-else if-else clause to
//avoid the overhead of having to perform 4 raycasts in a single frame
//for every AI player.
if (Physics.Raycast(puppet.transform.position, puppet.transform.forward, out tophit))
{
if (tophit.collider.tag == Tags.Region)
{
//We have an available target for our movement to the top of our
//current position.
_adjacentRegIdx++;
movement.Add(tophit.collider.gameObject.transform.position);
_adjacentRegions[_adjacentRegIdx - 1] = tophit.collider.gameObject.GetComponent<Region>();
}
}
else if (Physics.Raycast(puppet.transform.position, Vector3.left, out lefthit))
{
if (lefthit.collider.tag == Tags.Region)
{
//We have an available target for our movement to the left of our
//current position.
movement.Add(lefthit.collider.gameObject.transform.position);
_adjacentRegions[_adjacentRegIdx] = lefthit.collider.gameObject.GetComponent<Region>();
_adjacentRegIdx++;
}
}
else if (Physics.Raycast(puppet.transform.position, Vector3.right, out righthit))
{
if (righthit.collider.tag == Tags.Region)
{
//We have an available target for our movement to the right of our
//current position.
movement.Add(righthit.collider.gameObject.transform.position);
_adjacentRegions[_adjacentRegIdx] = righthit.collider.gameObject.GetComponent<Region>();
_adjacentRegIdx++;
}
}
else if (Physics.Raycast(puppet.transform.position, puppet.transform.forward * (-1), out bottomhit))
{
if (bottomhit.collider.tag == Tags.Region)
{
//We have an available target for our movement to the bottom of our
//current position.
movement.Add(bottomhit.collider.gameObject.transform.position);
_adjacentRegions[_adjacentRegIdx] = bottomhit.collider.gameObject.GetComponent<Region>();
}
}
_regionsIdentified = _adjacentRegIdx + 1;
}
which is used by my AI to get a sense of its current surroundings. For some reason, I kept getting NullReferenceException messages when I hit play on Unity. When I tried debugging with Visual Studio, I realized that in this code segment:
if (Physics.Raycast(puppet.transform.position, puppet.transform.forward, out tophit))
{
if (tophit.collider.tag == Tags.Region)
{
//We have an available target for our movement to the top of our
//current position.
movement.Add(tophit.collider.gameObject.transform.position);
_adjacentRegions[_adjacentRegIdx] = tophit.collider.gameObject.GetComponent<Region>();
_adjacentRegIdx++;
}
}
it skips the first two lines of code within the two if clauses and goes directly to the last ( _adjacentRegIdx++;
), causing the exception later on, when I use the array _adjacentRegions
. From what I know of programming so far this shouldn't be happening. Any ideas why it is?
P.S.:The code for class Region
works, I have already tested it thoroughly; but here it is anyway.
P.S.1: I use Visual Studio 2015 Enterprise Edition.
public class Region: MonoBehaviour
{
//Definition: A region is a polygonal area with a center, which
// provides data about the players traversing it.
public RegionBoundary[] boundaries;
/// <summary>
/// The center of the region, which is defined as the position
/// of the parent game object.
/// </summary>
public Vector3 RegionCenter
{
get
{
return transform.position;
}
}
private List<GameObject> members;
void Awake()
{
members = new List<GameObject>();
}
/// <summary>
/// Updates the region with any entries or exits (of any AI or of the player).
/// </summary>
/// <param name="unit">One of the AI or the player.</param>
public void UpdateRegion(GameObject unit)
{
if(!members.Contains(unit))
{
if (unit.tag == Tags.AIPlayerController)
unit.GetComponent<AIPlayerController> ().CurrentRegion = this;
else
unit.GetComponent<PlayerController> ().CurrentRegion = this;
members.Add(unit);
}
else
{
if (unit.tag == Tags.AIPlayerController)
unit.GetComponent<AIPlayerController> ().CurrentRegion = null;
else
unit.GetComponent<PlayerController> ().CurrentRegion = null;
members.Remove(unit);
}
}
/// <summary>
/// Returns all units in this region
/// </summary>
public List<GameObject> UnitsInRegion()
{
return members;
}
}
I believe it's skipping lines because your null reference is in the editor not in the code. Does Tags.Region even exist?
It is a static readonly string
member of the Tags class. Besides, if it didn't exist, the game wouldn't have gone to play mode at all.
And I have checked the debugger; it skips the movement.add()
and the _adjacentRegions[_adjacentRegIdx] = ...
lines and goes straight to _adjacentRegIdx++;
.
PS: The Tags.AIPlayerController
and Tags.PlayerController
variables are also static readonly string
members of the Tags
class.
PS 2: The exception I get directs me to the first place, in which I attempt to manipulate the _adjacentRegions array (which has null values since these lines are skipped). $$anonymous$$y question here was whether anyone has noticed this behaviour (i.e. skipping code lines for no apparent reason) before, and can enlighten me as to why it happens.
Don't trust the debugger 100%. I've had it many times that the debugger stops on the wrong lines because it was just bugging out or because somehow unity failed to compile my latest changes into the build and the debugger was stopping on the line numbers of the old code version but was showing me the new version.
Your answer
Follow this Question
Related Questions
how can i attach visual studio debug under android 2 Answers
Can you debug Unity 5.3.4f1 with VS 2013 Community? Everything else works. 1 Answer
How to make remote debugging with Visual Studio work? 0 Answers
Visual Studio Mac and Problem with Debugging 0 Answers
Visual Studio has stopped listing my Android device for debugging 1 Answer