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 BBrown4 · Aug 23, 2014 at 01:21 PM · c#errorcollider

Selecting Object From Top Causes NullReference

This is a very odd situation I am faced with here. I've been working on an RTS style selection, with the help of a tutorial from YouTube, for a game I am working on with a friend. I've never created a building placement and selection system before so off to Google and YouTube I went. Anyhow, a result I didn't expect has arisen. In it's simplest form, clicking one of the sides of a cube placed down, selects it as expected. However clicking the top of the cube returns a NullReferenceException.

I am totally baffled, I have googled the issue with no luck, I've even attached the debugger from MonoDevelop to the Unity editor so I can step through the code and see what's going on. That hasn't helped me narrow it down either. Below you will find a couple screenshots, and the code.

alt text

As you can see in the image above, the cube has been selected via one of the sides, it is selected properly, no errors.

alt text

And now, the image above shows the cube being selected from the top, however it returns a NullReferenceException. Below is the entire source code (well the relevant scripts at least)

BuildingPlacement.cs

 using UnityEngine;
 using System.Collections;
 
 public class BuildingPlacement : MonoBehaviour {
 
     private PlaceableBuilding placeableBuilding;
     private Transform currentBuilding;
     private bool hasPlaced = false;
 
     public LayerMask buildingsMask;
     public PlaceableBuilding placeableBuildingOld;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         Vector3 m = Input.mousePosition;
         m = new Vector3(m.x,m.y,transform.position.y);
         Vector3 p = camera.ScreenToWorldPoint(m);
         
         if(currentBuilding != null && !hasPlaced) {
             currentBuilding.position = new Vector3(p.x,0,p.z);
 
             if(Input.GetMouseButtonDown(0)) {
                 if(IsLegalPosition()) {
                     hasPlaced = true;
                 }
             }
         }
         else {
             if(Input.GetMouseButtonDown(0)) {
                 RaycastHit hit = new RaycastHit();
                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                 Debug.DrawRay(ray.origin, ray.direction * 100, Color.red, 15);
 
                 if(Physics.Raycast(ray, out hit, Mathf.Infinity,buildingsMask)) {
                     if(placeableBuildingOld != null) {
                         placeableBuildingOld.SetSelected(false);
                     }
                     hit.collider.gameObject.GetComponent<PlaceableBuilding>().SetSelected(true);
                     placeableBuildingOld = hit.collider.gameObject.GetComponent<PlaceableBuilding>();
                 }
                 else {
                     if(placeableBuildingOld != null) {
                         placeableBuildingOld.SetSelected(false);
                     }
                 }
             }
         }
     }
 
     bool IsLegalPosition() {
         if(placeableBuilding.colliders.Count > 0) {
             Debug.Log("Cannot place building(s), too close!");
             return false;
         }
         else {
             return true;
         }
     }
 
     public void SetItem(GameObject b) {
         hasPlaced = false;
         currentBuilding = ((GameObject)Instantiate(b)).transform;
         placeableBuilding = currentBuilding.GetComponent<PlaceableBuilding>();
     }
 }


The only relevant piece of code from this next script is the SetSelected function

PlaceableBuilding.cs

     public void SetSelected(bool s) {
         isSelected = s;
     }
 }
 

The line of interest in the NullReferenceException is this one:

 hit.collider.gameObject.GetComponent<PlaceableBuilding>().SetSelected(true);


I am totally stumped on this. Any and all help is deeply appreciated.

cubeselected.png (179.8 kB)
cubeselectedfromtop.png (163.5 kB)
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 robertbu · Aug 23, 2014 at 01:31 PM 0
Share

It is likely that you are hitting something other than you expect with your Raycast(). Add:

   Debug.Log(hit.collider.name + ", " + hit.collider.tag);

....as the first line inside your Raycast() success code.

avatar image BBrown4 · Aug 23, 2014 at 09:08 PM 0
Share

Ah yeah see I tried that before only I must have put it in the wrong place because it had returned a NullReferenceException and no name. This time it worked though and the issue has been resolved. Thanks. I'll post an answer of what happened and how it was fixed.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by BBrown4 · Aug 23, 2014 at 09:59 PM

Alright, so with the help of robertbu I've solved the issue. What was happening was that my "buildings" are in empty gameobjects (so i can set the building's y value to 0 and have the building NOT in the ground), and these containers actually have colliders on them that act as triggers. This triggers are bigger than the object themselves, which also have regular colliders on them so they can actually enter the triggers.

On the cube however, the height of the trigger collider was the same size as the regular collider. The script is attached to the container, not the object within the container so it was hitting the regular collider before the trigger collider, and as a result returned the null reference exception because that object was unrecognized. It's not too likely that anyone will run into this EXACT issue because it's a matter of how it was set up, but just on the off chance that it does happen, here is what was wrong with mine. Thanks robertbu for your help!

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

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

2 People are following this question.

avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

"Strange behaviour may occur" when using Network.AllocateViewID() 0 Answers

NullReferenceException problem 2 Answers

Help With AI Error 2 Answers

Need help calling a script to another keep getting errors 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