Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Jer_J · Jul 18, 2015 at 11:41 AM · raycastnullreferenceexceptiontag

Why am I getting NullReferenceException: Object Reference...

 void ScanTerrain(){
 
         
         RaycastHit detectHeight;
 
         stepHeight = stepHeight + 1; //Ensure ray starts below step
         wallHeight = wallHeight + 1; //Ensure ray starts below wall
 
         Vector3 rayOrigin = new Vector3(0,15f,0); 
         Vector3 centrePoint = new Vector3 (0, 0, 0); 
 
         bool checkIfSecondHit = false; 
 
 
         for (int x = 1; x < (terrainX*2); x = x + moveUnit) {
             rayOrigin.x = x; 
             for(int z = 1; z < (terrainZ*2); z = z + moveUnit){
                 rayOrigin.z = -z; 
 
                 //Send first ray and find centre point of tile
                 if (Physics.Raycast (rayOrigin, rayDirection, out detectHeight)){
                     centrePoint.x = rayOrigin.x; 
                     centrePoint.z = rayOrigin.z; 
                     centrePoint.y = rayOrigin.y - detectHeight.distance; 
                     ComputeMesh (centrePoint); 
 
                     Debug.Log (detectHeight.transform.tag); 
                     Debug.Log (rayOrigin); 
                     //Compute second ray depending on height info of features by setting origin of second ray
                     if (detectHeight.transform.tag == "Ruins"){
                         rayOrigin.y = rayOrigin.y - detectHeight.distance; 
                         if (Physics.Raycast(rayOrigin, rayDirection, out detectHeight)){
                             centrePoint.x = rayOrigin.x; 
                             centrePoint.z = rayOrigin.z; 
                             centrePoint.y = rayOrigin.y - detectHeight.distance; 
                             ComputeMesh (centrePoint); 
                             checkIfSecondHit = true;
                         }
                     }
                     if (detectHeight.transform.tag == "Wall" && !checkIfSecondHit){
                         rayOrigin.y = rayOrigin.y - (detectHeight.distance + wallHeight); 
                         if (Physics.Raycast(rayOrigin, rayDirection, out detectHeight)){
                             centrePoint.x = rayOrigin.x; 
                             centrePoint.z = rayOrigin.z; 
                             centrePoint.y = rayOrigin.y - detectHeight.distance; 
                             ComputeMesh (centrePoint); 
                             checkIfSecondHit = true; 
                         }
                     } 
 
                     if (detectHeight.transform.tag == "StoneStep" && !checkIfSecondHit){
                         rayOrigin.y = rayOrigin.y - (detectHeight.distance + stepHeight); 
                         if (Physics.Raycast(rayOrigin, rayDirection, out detectHeight)){
                             centrePoint.x = rayOrigin.x; 
                             centrePoint.z = rayOrigin.z; 
                             centrePoint.y = rayOrigin.y - detectHeight.distance; 
                             ComputeMesh (centrePoint); 
                             checkIfSecondHit = true; 
                         }
                     }
 
 
 
 
                     //Reset initial Ray height value
                     rayOrigin.y = 15f; 
                     checkIfSecondHit = false; 
 
                 }
 
             }
 
         }
 
     }
 
 
     void ComputeMesh(Vector3 tileCentre){
 
 
         //Initialize variables
         Vector3 vertRay = new Vector3 (0,0,0); 
         RaycastHit vertexHeight; 
         vertRay.y = tileCentre.y; 
         Vector3[] vertices = new Vector3[4]; 
         int cornerCount = 0; 
 
         Dictionary<Vector3, int> tileLookUp = new Dictionary<Vector3, int> (); 
         List<TileMapObject> tileList = new List<TileMapObject> (); 
 
 
         //So 4 vertex points means 4 coordinates. Since I have the centre coordinate from tileCentre I can compute the vertex from it
         for (int vx = -1; vx < 1; vx = vx + moveUnit) {
             for (int vz = -1; vz < 1; vz = vz + moveUnit){
             
                 vertRay.x = tileCentre.x - vx; 
                 vertRay.z = tileCentre.z - vz; 
 
                 if (Physics.Raycast(vertRay, rayDirection, out vertexHeight)){
 
                     vertices[cornerCount].x = vertRay.x; 
                     vertices[cornerCount].z = vertRay.z; 
 
                     vertices[cornerCount].y = vertRay.y - vertexHeight.distance; 
 
                     tileList.Add(new TileMapObject(vertices)); 
                     tileLookUp.Add(tileCentre, tileList.Count); 
 
                     //cornerCount is to keep track of the vertices for the triangle 
                     cornerCount++; 
 
 
 
 
                 }
             }
         }
 

I get the error on the if statement lines in ScanTerrain()

NullReferenceException: Object reference not set to an instance of an object

LayGrid.ScanTerrain () (at Assets/Scripts/LayGrid.cs:60)

LayGrid.Start () (at Assets/Scripts/LayGrid.cs:17)

Which is at: if (detectHeight.transform.tag == "Wall" && !checkIfSecondHit)

When I check the console debug the coordinates at which it fails is over an object tagged Ruins which has nothing underneath it so it should return false for that if statement, why would it give me this error if the if statement is false at that point?

Thanks

EDIT:: Forgot to mention that this loop runs successfully many times before running into an error. The loop gets stuck at a seemingly arbitrary coordinate, when I changed the order of if statements the coordinate at which it gets stuck changes. However in both cases the tag at which it fails is not the one being referred to by the error.

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 Positive7 · Jul 18, 2015 at 12:53 PM 0
Share

What's Start 17th line?

avatar image Jer_J · Jul 18, 2015 at 08:45 PM 0
Share

Its the line at which ScanTerrain(); is called, the only thing before it is object initialization for one object which is done through the normal

 public GameObject terrain; 
 
 void Start(){
 
 terrain = GetComponent<GameObject>(); 
 
 ScanTerrain(); //line 17
 
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RLin · Jul 18, 2015 at 01:35 PM

The GameObject detectHeight does not exist. Remember that when using GameObject.Find(), it is case-sensitive, so this may be why it returns null.

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 Positive7 · Jul 18, 2015 at 01:40 PM 0
Share

RaycastHit detectHeight;

avatar image Jer_J · Jul 18, 2015 at 07:19 PM 0
Share

Yeah detectHeight is a RaycastHit, so it should be returning the tag of the object it hits. Which it does successfully many times before running into a random NullReferenceException error at an see$$anonymous$$gly arbitrary point. I've also tried changing the order of the if statements at it gives me a NullReferenceException at another "arbitrary" point on the map. Not sure what's happening here...

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Raycasting error 1 Answer

RaycastHit2D.collider is null - why? 1 Answer

Raycast detection / lag issue 0 Answers

Unity RaycastHit.transform.tag working in editor not working on Android 1 Answer

Getting a Raycast to Classify Objects as the Same Thing 0 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