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 /
avatar image
-1
Question by putlucky · Feb 23, 2017 at 05:12 PM · c#nullreferenceexceptionselection

Need a little help with this nullrefexception.

So these pop up if something is missing a reference, but it's sometimes difficult to see what exactly the problem is. So I'll quickly explain my script:

Left clicking on a selectable object (predicated by the public list), will cause selected to be true and return that selectable object's corresponding 'target' gameobject. I'm moving the selectable objects around using target go's for them to follow. The right clicking will move the chosen target to the mouse cursor position.

The nullreference exception that occurs at runtime refers to this line: target.transform.position = hit.point;

 public class Movement : MonoBehaviour {
 
     //list of the targets to move
     public List<GameObject> targets = new List<GameObject>();
 
     //list of the gameobjects to be selected
     public List<GameObject> units = new List<GameObject>();
 
     public bool selected = false;
 
 
     public Camera camera;
     private GameObject target;
 
 
     
 
     void Update () {
 
         Select();
 
         if (selected == true)
         {
             SetTarget();
         }
     }
 
     public GameObject Select()
     {
         if (Input.GetMouseButtonDown(0))
         {
             RaycastHit hit;
 
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
             if (Physics.Raycast(ray, out hit))
             {
                 foreach (GameObject unit in units)
                 {
                     if (hit.collider.gameObject == unit)
                     {
                         selected = true;
                     }
                     else { selected = false; }
                 }
                 foreach (GameObject targ in targets)
                 {
                     if (hit.collider.gameObject.tag == targ.tag)
                     {
                         return targ;
 
                     }
 
 
                 }
 
 
 
             }
 
         }
         return null;
 
 
     }
 
 
     public void SetTarget()
     {
         Debug.Log("selected");
         
         target = Select(); 
 
             if (Input.GetMouseButtonDown(1))
             {
                 RaycastHit hit;
                 Ray ray = camera.ScreenPointToRay(Input.mousePosition);
 
                 //Raycast
                 if (Physics.Raycast(ray, out hit))
                 {
                     target.transform.position = hit.point;
                 }
             }
 
     }
 }
 

Is the if statement for this:

 if (hit.collider.gameObject == unit)
                      {
                          selected = true;
                      }



returning true and therefore exiting? If so, how would I get around this?

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 hexagonius · Feb 23, 2017 at 06:02 PM 0
Share

you have two returns in Select(). return targ and return null. Since SetTarget is calling it and assigning the result to target it must be null because hit is a struct and therefore cannot be null.
You should check target for null and either return or fix the problem another way.

avatar image putlucky hexagonius · Feb 23, 2017 at 07:21 PM 0
Share

@hexagonius Hey, so wait why does hit being unable to be null mean that target must be null. And what would cause it to be null?

avatar image Hanoble putlucky · Feb 23, 2017 at 07:53 PM 0
Share

In your own Select() method you are checking if the mousebutton is down and then checking to see if the raycast hit. If the raycast does not get a hit, you are returning null. Which means that when you run your SetTarget() method and have:

 target = Select();

Your object returned in the case of no raycast hit is null. In the case of target being null, when you attempt to set the position of the target to that of the hit point, you are trying to access the transform of a null object and are getting a null reference exception.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Hanoble · Feb 23, 2017 at 07:45 PM

You need to make sure that the target is not null before assigning the position of it to the hit point. Since Select() can return null, when it does, you will be attempting to access the transform and set the position of a null object. One way to fix this is to do a null check at this line here:

 //Raycast and ensure target returned from select was not null
  if (Physics.Raycast(ray, out hit) && target != null)
  {
       target.transform.position = hit.point;
  }
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 putlucky · Feb 23, 2017 at 08:28 PM 0
Share

This has cleared my nullreference exception but I still don't understand why it keeps being null. I'll select a gameobject within the list and nothing will happen.

avatar image Hanoble putlucky · Feb 23, 2017 at 09:56 PM 0
Share

I am not sure if you are aware of how to debug unity with Visual Studio (or monodevelop), but if not, here is a good tutorial. This will allow you to actually put breakpoints in your code and stop execution at specific points. It will help you to better understand what is happening when you get null returned.

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

287 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 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 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 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 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 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 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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

3D Platformer Tutorial getting Lerpz's SkinnedMeshRenderer using c# 0 Answers

Object reference not set to an instance of an object... 0 Answers

NullRefenceException error 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