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
0
Question by natestale · Apr 02, 2016 at 10:33 PM · nullreferenceexceptionsendmessage

Null Reference Exception error on GameObject.SendMessage

Hey guys, i have this script to pickup items, and works perfectly, but this Null Reference Exception error comes from line 60 and i don't know the reason. Any help please ?

 using UnityEngine;
 using System;
 using System.Collections;
 using System.Collections.Generic;
 
     [System.Serializable]
     public class TagsClass{
         public string m_InteractTag = "Interact";
         public string m_InteractItemsTag = "InteractItem"; 
         public string m_DoorsTag = "Door"; 
     }
 
 public class DragRigidbodyUse : MonoBehaviour {
     
     public GameObject playerCam;
     
     public string UseButton = "Use";
     public TagsClass Tags = new TagsClass();
     
     private float PickupRange = 2f;
     
     private Ray playerAim;
     private GameObject objectHeld;
     private bool tryPickupObject;
     
     void Start () {
         tryPickupObject = false;
         objectHeld = null;
     }
     
     void FixedUpdate () {
         if(Input.GetButton(UseButton)){
             tryPickObject();
             tryPickupObject = false;
             Use();
         }
     }
     
     private void tryPickObject(){
         Ray playerAim = playerCam.GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
         RaycastHit hit;
         
         if (Physics.Raycast (playerAim, out hit, PickupRange)){
             objectHeld = hit.collider.gameObject;
             if(hit.collider.tag == Tags.m_InteractTag && tryPickupObject){
                 objectHeld.GetComponent<Rigidbody>().useGravity = false;
                 /**/
             }
             if(hit.collider.tag == Tags.m_InteractItemsTag && tryPickupObject){
                 objectHeld.GetComponent<Rigidbody>().useGravity = true;
             }
             if(hit.collider.tag == Tags.m_DoorsTag && tryPickupObject){
                 objectHeld.GetComponent<Rigidbody>().useGravity = true;
                 objectHeld.GetComponent<Rigidbody>().freezeRotation = false;
             }
         }
     }
     
     private void Use() {
         objectHeld.SendMessage ("UseObject",SendMessageOptions.DontRequireReceiver); //Every script attached to the PickupObject that has a UseObject function will be called.
         objectHeld = null;    
     }
 }
 
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 gjf · Apr 02, 2016 at 05:07 PM 0
Share

you're assu$$anonymous$$g that objectHeld is being set - it's probably not. try doing a null check before using it...

avatar image natestale · Apr 03, 2016 at 07:09 PM 0
Share

is being set only when the raycast hit's the object with tag "InteractItem"

1 Reply

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

Answer by miszelfox · Apr 03, 2016 at 07:42 PM

Null Reference means that objectHeld is null at this moment. Call your method ONLY when you are sure that it's not empty:

 if(objectHeld!=null)
 Use();

You call your method everytime button is pressed, but your objectHeld is not always set. You can also just put this method call in your tryPickObject method after first if statement

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 natestale · Apr 03, 2016 at 09:52 PM 0
Share

Thanks dude, works perfect !!!

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

NullReferenceException when sending message to another object...really stumped. 1 Answer

Functions work in the Editor, don't work when Built. (Javascript) 0 Answers

SendMessage weird runtime error 2 Answers

Remmember the spawner 2 Answers

location of game object on x axis 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