Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Raspilicious · Jul 18, 2012 at 09:43 AM · tilenull reference exceptionhexagonnode

How to fix this null reference exception?

Hey people!

I've been working with the 'Tile Based Map & Nav' package with a honeycomb grid, and am trying to make a system that checks all nodes on the map (40x40, 1600 nodes), detects obstacles, and edits the properties of the neighbours of those objects.

This is what I have so far, but I'm completely stuck as to why it keeps giving me the same error. It's one of the 'null reference exception' errors, referring to my variable 'node'.

Here's the code:

 public MapNav theTileMap;
 
 void setGiant () {
     Debug.Log("setGiant initiated");
     // run this class at end of each unit's movement
 
     float offsetY = 2f;
     RaycastHit hit;
     foreach (TileNode n in theTileMap.nodes) {
         if (Physics.Raycast(n.gameObject.transform.position, Vector3.up, out hit, offsetY * 4f)) {
             TileNode node = hit.collider.GetComponent<TileNode>();
             if (node.gameObject.layer > 20) {
                 Debug.Log("Hit unit " + hit.collider.gameObject.name + "on layer " + hit.collider.gameObject.layer);
                 foreach (TileNode n2 in node.nodeLinks) {
                     if (n2.IsDirectNeighbour(node)) {
                         Debug.Log("n2's name: " + n2.name);
                     }
                 }
             }
         }
     }
     howLongDidItTake -= 2;
     Debug.Log("time it took: " + howLongDidItTake);
     countUp = false;
 }

The 'howLongDidItTake' timer just shows exactly that, running through 1600 nodes I thought it would be neat to know how long it would take. It hits between 0.001 and 0.0005 seconds on average.

Thanks to anyone in advance who can shed some light on this problem.

Edit x4: Replaced the 'quot&;' with " marks... and added in the mysteriously-disappeared < TileNode> after the GetComponent line.

Comment
Add comment · Show 2
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 cupsster · Jul 18, 2012 at 09:50 AM 0
Share

please format your code properly if you want any kind of reply. It is VERY hard to read.

avatar image Raspilicious · Jul 18, 2012 at 09:53 AM 0
Share

Sorry, I realised it had messed up the formatting as soon as I posted it, and I was fixing it as you commented.

edit: Edited twice, as it replaced " marks with '& quot ;' or something silly.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by HazeTI · Jul 18, 2012 at 10:16 AM

The error is on this line

 TileNode node = hit.collider.GetComponent();

In the documentation it states "Returns the component of Type type if the game object has one attached, null if it doesn't". So it can return null first you should check that your node is not null before using it.

 if (node != null && node.gameObject.layer > 20)

Secondly, I believe you are also using GetComponent() incorrectly by not specifying what type you are looking for. Try changing your code slightly to this and see if this works

 TileNode node = hit.collider.GetComponent<TileNode>();


Comment
Add comment · 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
0

Answer by Bunny83 · Jul 18, 2012 at 10:20 AM

You raycast upwards and just guess that the object, what ever it is, has a TileNode script attached. You should either check if the hitted object has such a component or put all nodes in a seperate layer and use a layermask in your raycast.

It seems you already playing around with layers, so setup a layermask for the Raycast. This will even increase the performance when you ignore other stuff.

Comment
Add comment · Show 3 · 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 Raspilicious · Jul 18, 2012 at 10:26 AM 0
Share

The nodes themselves don't actually have colliders though, so maybe I have to approach it from an entirely different angle...

avatar image Bunny83 · Jul 18, 2012 at 10:32 AM 0
Share

They don't have a collider? Then why do you raycast and expect a TileNode on the object?

This seems a bit strange ;) $$anonymous$$aybe some explanations on the actual aim of this?

avatar image Raspilicious · Jul 18, 2012 at 10:37 AM 0
Share

I was intending to do the raycast to see if there's an object sitting on a tile, and by accessing the node of the tile, and then that node's neighbouring nodes, I could change those neighbour's properties (put it on or remove it from the 'giant' layer).

It's basically so I can have large units (a 7-hex-cluster sized unit) moving around the map on their own layer. When they come up to a wall/object/unit, that object has a buffer of hexes around it that the large unit cannot path on to, so it doesn't clip through the sides of a wall.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Hexagonal grid with 120 degree angles 1 Answer

how to implement hexagonal tiles? 3 Answers

An arrow pointing from one hexTile to another hexTile 0 Answers

Tiling textures in Unity for Iphone? 2 Answers

Texturing Help 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