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 StetsonT22 · Jan 09, 2016 at 05:01 AM · c#null reference exceptionavoidance

Why wont my code run?

After trying to fix a null reference error my code no longer runs. I threw in some debug.logs to see what was going on, and it wont make it passed the second debug.log.

I'm confused, by the third frame/timestep would all conditions not be met?

 /*Item Pickup Ray*/
 /*To be attached to the player CAMERA...*/
                         /*Stetson*/    //25/12/2015
 using UnityEngine;
 using System.Collections;
 
 public class ItemPickupRay : MonoBehaviour {
 
     public float pickupDistance = 2f; //Max distance that an object can be picked up from...
     public LayerMask layerMask; //Go in the inspecter and set this to the same layer all pickupable object...
 
     private GameObject cachedOBJ;
     public ItemDisplayInfoManager cachedMNGR;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void FixedUpdate () {
 
         //Create ray...
         Ray pickupRay = new Ray (transform.position, transform.forward);
         //Create a veriable to store the hit object...
         RaycastHit hit;
         //if our ray hits something we'll store the hit info...
         if (Physics.Raycast (pickupRay, out hit, pickupDistance, layerMask)) {
 
             Debug.Log ("hit Something on layer");
 
             //Store the object we are hitting...
             cachedOBJ = hit.collider.gameObject.transform.parent.gameObject;
 
             //if there is a game objects stored already...
             if (cachedOBJ){
 
                 Debug.Log ("Have object cached");
 
                 //Store the object we are hittings manager...
                 cachedMNGR = hit.collider.GetComponentInChildren<ItemDisplayInfoManager>();
 
                 //If we have a manager...
                 if(cachedMNGR){
 
                     Debug.Log ("Have manager cached");
 
                     //If hit object is a weapon...
                     if (cachedOBJ. GetComponent<Firearm>()){ 
 
                         Firearm fireArmScript = cachedOBJ.GetComponent<Firearm>();
 
                         //If the weapon is NOT in the players hand...
                         if(!fireArmScript.isInHand){
 
                             //Display weapon info...
                             cachedMNGR.ShowInfo();
                     
                             //if user presses the "USE" button...
                             if (Input.GetButtonDown ("Use/Select")){
 
                                 //Call PickupWeapon() Method...
                                 fireArmScript.PickupWeapon ();
                             }
 
                          }
 
                      }     
                 }
 
             }
 
          }
 
      }
 }
 
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 bubzy · Jan 09, 2016 at 09:09 AM 0
Share
  //Store the object we are hitting...
  cachedOBJ = hit.collider.gameObject.transform.parent.gameObject;
  
 Debug.Log(cachedOBJ); // add this line
 
  //if there is a game objects stored already...

try this, it will at least tell you if cachedOBJ is being used properly

0 Replies

· Add your reply
  • Sort: 

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

56 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

Related Questions

NullReferenceExeption mais variable initialisée 1 Answer

GameObject only spawning 60% of the time (c#) 1 Answer

GetComponnent null reference C# 1 Answer

Struggling with Unity 5 GetComponent: Null reference 0 Answers

Null Reference but Referenced 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