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 david_kuehn · Jun 12, 2016 at 02:41 PM · snapsnappingobject referenceobject-reference-error

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

Hi everybody, I'm creating a script that allows an object to "snap" to the position of another object when dropped close enough to it. I have multiple locations that the object can be snapped to, and I created a method to sort through all of the possible snap locations and find which one is closest to the current player position. However, when trying to snap to a particular location, I get an Object Reference error in line 41 of the script which reads transform.position = FindClosestPartner().transform.position;. Here is the full script.

 using UnityEngine;
 using System.Collections;
 
 public class DotRearrangement : MonoBehaviour
 {
 
     //Initializes all Snapping capabilities
     public float closeDist = 3.5f;
     public float dist = Mathf.Infinity;
     public float moveSpeed;
     public float rotationSpeed;
 
     public GameObject FindClosestPartner()
     {
         GameObject[] partners;
         partners = GameObject.FindGameObjectsWithTag("SnapPartner");
         GameObject closest = null;
         Vector3 myPosition = transform.position;
         foreach (GameObject partner in partners)
         {
             Vector3 diff = partner.transform.position - myPosition;
             float curDistance = diff.sqrMagnitude;
             if (curDistance < dist)
             {
                 closest = partner;
                 dist = curDistance;
             }
         }
         return closest;
     }
 
     void OnMouseDrag()
     {
         Vector3 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         point.z = transform.position.z;
         transform.position = point;
     }
 
     void OnMouseUp()
     {
         transform.position = FindClosestPartner().transform.position;
     }
 }

Any help would be appreciated :)

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by HaykAv · Jun 12, 2016 at 02:54 PM

@Arctinium I think you can fix the problem by making your FindClosePartner method return a Vector3 not a GameObject.

 public Vector3 FindClosePartner() {
     //Your code goes here and then at the end you just return the closest GameObject's position
     return closest.transform.position;
 }

 void OnMouseUp()
 {
      transform.position = FindClosestPartner();
 }

Tell me if this worked or no.

Comment
Add comment · Show 1 · 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 david_kuehn · Jun 12, 2016 at 03:26 PM 0
Share

@HaykAv Okay, I tried doing that and rewriting line 41 to look like transform.position = FindClosestPartner(); but it still gives me the error when I run the game every time I release the mouse button. Any ideas? Also thanks for the quick response!

avatar image
0

Answer by Mmmpies · Jun 12, 2016 at 04:07 PM

Create a new variable

 public Camera myCam;

Drag the main Camera onto it and replace

 Vector3 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);

with

 Vector3 point = myCam.ScreenToWorldPoint(Input.mousePosition);

Would have been easier if you'd posted the line number but hey you know for next time :)

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 david_kuehn · Jun 12, 2016 at 05:40 PM 0
Share

Hi @$$anonymous$$mmpies , I've done as you said, it is still giving me the same error, but now it's only when the mouse button is released and the player is too far away to snap to a location. When it snaps, no error is given. Would it be because the method is not returning any value when it doesn't detect an object close enough to snap to? The error is at line 30:

 using UnityEngine;
 using System.Collections;
 
 public class DotRearrangement : $$anonymous$$onoBehaviour
 {
 
     //Initializes all Snapping capabilities
     public float closeDist = 3.5f;
     public float dist = $$anonymous$$athf.Infinity;
     public float moveSpeed;
     public float rotationSpeed;
     public Camera myCam;
 
     public Vector3 FindClosestPartner()
     {
         GameObject[] partners;
         partners = GameObject.FindGameObjectsWithTag("SnapPartner");
         GameObject closest = null;
         Vector3 myPosition = transform.position;
         foreach (GameObject partner in partners)
         {
             Vector3 diff = partner.transform.position - myPosition;
             float curDistance = diff.sqr$$anonymous$$agnitude;
             if (curDistance < dist)
             {
                 closest = partner;
                 dist = curDistance;
             }
         }
         return closest.transform.position; //Error is here
     }
 
     void On$$anonymous$$ouseDrag()
     {
         Vector3 point = myCam.ScreenToWorldPoint(Input.mousePosition);
         point.z = transform.position.z;
         transform.position = point;
     }
 
     void On$$anonymous$$ouseUp()
     {
         transform.position = FindClosestPartner();
     }
 }


avatar image Mmmpies david_kuehn · Jun 12, 2016 at 06:02 PM 0
Share

Which is your return line so it's a different cause even if the error remains the same.

Best guess is how many objects have you actually tagged SnapPartner? I'm betting zero so far as the test scene I created worked but then I'd tagged 2 other objects with that tag.

EDIT - Sorry are you saying it sometimes works? If you set dist to infinity how can it not find something closer or do you try one snap and then try another and dist is still holding the previous value of the closest, which may be less than any available option, if so reset dist to infinity before running the foreach loop.

avatar image david_kuehn Mmmpies · Jun 13, 2016 at 04:56 PM 0
Share

@$$anonymous$$mmpies I currently have 6 objects tagged SnapPartner. I do not get any errors when the object successfully snaps to a SnapPartner, but when the object unsuccessfully snaps, I get an error. Sorry for being rather unclear with my earlier comment. I tried resetting dist to infinity before the foreach loop, but then it only snaps to one particular SnapPartner, even if it is not the closest.

EDIT: Is there a way to return another value if there is no object close enough to snap to? I think the error is because the if statement doesn't get called because nothing is close enough to snap to, and in that if statement, closest is being defined, and then it is trying to return closest which doesn't exist. Does this make any sense or is it completely unrelated?

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

58 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

Related Questions

snapping the Players psosition to another GameObject for a "Ledge grab" 0 Answers

Object reference not set to instance of an object? 1 Answer

Snapping using kinect v2 sdk 1 Answer

Why is object reference not set to an instance? 1 Answer

true grid snapping in editor ? 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