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 Chickenator · Jan 02, 2015 at 12:39 PM · pickupobject-reference-errorobject-reference-not-set

Object Reference Not Set Up As An Instance Of An Object

Full error: NullReferenceException: Object reference not set to an instance of an object PickUpScript.Update () (at Assets/PickUpScript.js:43)

So this script is meant to pick up and drop items when clicked on. When I pick up the item I get that error. Here's the script

 #pragma strict
 
 var hitObject : GameObject;
 var pickupPosition : Transform;
 var hold : boolean = false;
 var pickableObject : GameObject[];
  
  function Update () {
 
     if(Input.GetMouseButtonDown(1)) {
     
         var hit : RaycastHit;
             if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit)) {
             
                 hitObject = hit.collider.gameObject;
                     
                     if (hitObject.tag == "pickUp") {
                     
                         var kinematic : Rigidbody;
                         kinematic = hitObject.gameObject.GetComponent (Rigidbody);
                         hitObject.transform.position = pickupPosition.position;
                         hold = true;
                         kinematic.isKinematic = false;
                         
                         
                     
                     }
             
             
             }
             
             
     
     }
     
     if (hold == true) {
     
         hitObject.transform.position = pickupPosition.position;
     
     }
     
         if (Input.GetMouseButtonUp(1)) {
             kinematic.isKinematic = true;
             hold = false;
         
         }
 
         if (Input.GetMouseButtonUp(0)) {
         
             Debug.Log ("fire");
         
         }
 
 }

How should I change the script so I don't get the error?

Thanks in advance.

(Sorry this is like my 5th question today xD)

Comment
Add comment · Show 1
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 Landern · Jan 02, 2015 at 12:52 PM 0
Share

So if you hit the button on the mouse but the raycast is false, your kinematic variable will be null, however if you release the mouse button it will try and set it to true for the field/property is$$anonymous$$inematic, you need to work the logic out better.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ekta-Mehta-D · Jan 02, 2015 at 12:46 PM

hii..

Update this code and check u still getting the error.

 if (hold == true) {
 
    if(hitObject)
   {
          hitObject.transform.position = pickupPosition.position;
   }  

}

You might have error in kinematic variable. Declare that variable out of the function. and check condition whether it is null or not.

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 Chickenator · Jan 02, 2015 at 12:49 PM 0
Share

Yeah I replaced it with that and still got the same error

avatar image Ekta-Mehta-D · Jan 02, 2015 at 12:50 PM 0
Share

var kinematic : Rigidbody;

Declare this out of function.

 if (Input.Get$$anonymous$$ouseButtonUp(1)) {
            if(kinematic)
            {
              kinematic.is$$anonymous$$inematic = true;
              hold = false;
            }
          }

avatar image Chickenator · Jan 02, 2015 at 12:51 PM 0
Share

what do you mean by declare this out of class?

avatar image Ekta-Mehta-D · Jan 02, 2015 at 12:52 PM 0
Share

I mean in the area where u have declared all other variables.

avatar image Chickenator · Jan 02, 2015 at 12:56 PM 0
Share

It works without moving the variable but with one problem. When I let go of the object everything goes flying around and into space xD even though it's incredibly funny it's not exactly what I want. Any idea why this might happen?

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

27 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

Related Questions

Bug in Object reference ? 0 Answers

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

Accessing other script's variable into another script 0 Answers

Picking up/Holding objects? 4 Answers

Help with adding power ups to game 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