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 asdf123 · Mar 30, 2013 at 10:14 PM · referencenullitem

Need help with a raycast

Hi, i have wrote a script that when we click with Fire1 on an object that have the tag "Bottle" pick up them. it works perfectly then i added a code to calculate the distance from object and player, and only able player to pick up the item when we are 2 meters from the object, but i have an error:

NullReferenceException PlayerController.Update () (at Assets/Scripts/PlayerController.cs:15)

Script: using UnityEngine; using System.Collections;

 public class PlayerController : MonoBehaviour {
     
     private Transform pickObj = null;
     private RaycastHit hit;
     private Ray ray;
     private float distance;
     
     void  Update (){
     
             distance = Vector3.Distance(pickObj.transform.position, gameObject.transform.position);
         
             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (!pickObj) {
                 if (Physics.Raycast(ray, out hit) && hit.transform.tag == "Bottle") {
                     if(distance <= 2) {
                         if(Input.GetMouseButton(0)) {
                             pickObj = hit.transform;
                             
                 }
             }
             else {
                 pickObj.transform.active = false;
                 }    
             }
         }
     }
 }
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 joeyaaaaa · Mar 30, 2013 at 10:38 PM 0
Share

probable try saying input first then do the other stuffother wise your checking the distance alll the time and not just when the button is pressed

avatar image asdf123 · Mar 30, 2013 at 10:52 PM 0
Share

still have null reference exepcion

avatar image joeyaaaaa · Mar 30, 2013 at 10:59 PM 0
Share

the null reference is pointing to your update, follow it, read it, the error pasted doesnt give enough info for someone to help

1 Reply

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

Answer by Unitraxx · Mar 30, 2013 at 11:12 PM

You can't calculate the distance, if pickObj == null. Which will always be null, since you only set it to something else if the distance is smaller than 2. But since that can't be calculated, you have to place the distance calculation after the ray cast calculation.

Comment
Add comment · Show 8 · 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 asdf123 · Mar 31, 2013 at 12:07 AM 0
Share
 if(!pickObj) {
             distance = Vector3.Distance(pickObj.transform.position, gameObject.transform.position);
             if(Input.Get$$anonymous$$ouseButton(0)) {


still dont working :C

avatar image Unitraxx · Mar 31, 2013 at 12:11 AM 0
Share

What's your error now ? (You did remove the line at line 10 did you? Having it twice in there won't help :) )

avatar image asdf123 · Mar 31, 2013 at 12:24 AM 0
Share

i will put there the script how i have:

but now i have null reference exepcion in distance = Vector3.Distance... line

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : $$anonymous$$onoBehaviour {
     
     private Transform pickObj;
     private RaycastHit hit;
     private Ray ray;
     private float distance;
     
     void  Update (){
         
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         
         if(!pickObj) {
             distance = Vector3.Distance(pickObj.transform.position, gameObject.transform.position);
             if(Input.Get$$anonymous$$ouseButton(0)) {   
                 if (!pickObj) {
                     if (Physics.Raycast(ray, out hit) && hit.transform.tag == "Bottle") {
                         if(distance <= 2) {
                             
                             pickObj = hit.transform;
                                 
                     }
                 }
                 else {
                     pickObj.transform.active = false;
                     }    
                 }
             }
         }
     }
 }
avatar image Unitraxx · Mar 31, 2013 at 12:45 AM 0
Share

replace pickObj.transform.position at line 16 with pickObj.position. And also at line 27, pickObj.transform.active should be replaced with pickObj.active.

avatar image asdf123 · Mar 31, 2013 at 01:46 AM 0
Share

still have the error on

 distance = Vector3.Distance(pickObj.position, gameObject.transform.position);
Show more comments

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

12 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

Related Questions

Null reference when calling a method from another script 1 Answer

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

Simple Script getting Errors. 1 Answer

Why is the Object reference is empty ? 1 Answer

NullReferenceException error 2 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