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 /
  • Help Room /
avatar image
0
Question by AlexT_IH · Jan 25, 2016 at 01:48 PM · visual studiodebuggingunity 5.1.1

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;
     }
 }
Comment
Add comment · Show 3
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 Tactical_Beard · Jan 25, 2016 at 03:20 PM 0
Share

I believe it's skipping lines because your null reference is in the editor not in the code. Does Tags.Region even exist?

avatar image AlexT_IH Tactical_Beard · Jan 25, 2016 at 07:12 PM 0
Share

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

avatar image NoseKills · Jan 25, 2016 at 10:48 PM 1
Share

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.

0 Replies

· Add your reply
  • Sort: 

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

41 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

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


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