Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Why is this coming up null???

Discussion in 'Scripting' started by Not_Sure, Oct 27, 2020.

  1. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,546
    NullReferenceException: Object reference not set to an instance of an object
    Grab.Update () (at Assets/Grab.cs:25)

    I'm not getting why I'm getting told this is coming up null, I just gave "grabMe" a value!

    Code (CSharp):
    1.         RaycastHit hit;
    2.         if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, reach, layerMask))
    3.         {
    4.             GameObject item = hit.transform.gameObject;
    5.             GrabMe grabMe = item.GetComponent<GrabMe>();
    6.             grabMe.highLighted = true;
    7.         }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    After line 4, print the name of the GameObject it finds.

    Code (csharp):
    1. Debug.Log( item.name);
    Make sure it's what you expect, i.e, where the GrabMe is located.
     
    Not_Sure and Bunny83 like this.
  3. SpookyCat

    SpookyCat

    Joined:
    Jan 25, 2010
    Posts:
    3,748
    Probably because the hit object 'item' does not have a GrabMe component on it.
     
    Not_Sure, Bunny83 and Kurt-Dekker like this.
  4. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,546
    Ah, I'm thinking I'm having an LOD issue then.

    I attached the script to my LOD0.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    You could make a proxy common collider object just for this "hey you can grab me!" indicator... that way you don't have to clone it to all the LODs...
     
    Not_Sure likes this.
  6. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,546
    FPSController
    UnityEngine.Debug:Log(Object)
    Grab:Update() (at Assets/Grab.cs:24)
     
  7. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,546
    Maybe it's my masking?

    .Here's the entire code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Grab : MonoBehaviour
    6. {
    7.     public float reach;
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.        
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         int layerMask = 1 << 8;
    18.         layerMask = ~layerMask;
    19.        
    20.         RaycastHit hit;
    21.         if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, reach, layerMask))
    22.         {
    23.             GameObject item = hit.transform.gameObject;
    24.             Debug.Log(item.name);
    25.             GrabMe grabMe = item.GetComponent<GrabMe>();
    26.             grabMe.highLighted = true;
    27.         }
    28.     }
    29. }
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,893
    This looks to me like your raycast is probably hitting your player itself.
     
    Not_Sure likes this.
  9. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,546
    I tried adding a mesh collider to the LOD parent with the script and it's still coming back null.

    Also, this script is for picking up items so it should always be LOD0 simply because I'm limiting the range to 3 units.
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Isn't LOD based on scanline height?? I haven't used it in so long I forget...
     
    Not_Sure likes this.
  11. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,546
    OKAY WAIT!

    I'm a complete moron!!!

    I'm telling it to ignore only my layer. NOT to ignore everything BUT my layer.

    Bit shift is still so confusing to me still.

    Delete that one line of code that flips them and it works like a charm.

    Never would have seen it without the log suggestion.

    Thanks guys!
     
    PraetorBlue and Kurt-Dekker like this.
  12. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,546
    Pretty happy with the results:
     
  13. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Agreed... 10/10 would highlight again, definitely better than getting hit by a wooden club.
     
    Not_Sure likes this.