Search Unity

Resolved Component reference Error

Discussion in 'Scripting' started by devonmuhammad968, Jul 14, 2021.

  1. devonmuhammad968

    devonmuhammad968

    Joined:
    Sep 30, 2018
    Posts:
    41
    im having this error on my 2nd script when trying to access pickedup boolean

    : Object reference not set to an instance of an object
    SetActive.OnCollisionEnter (UnityEngine.Collision col)

    Code (CSharp):
    1.  
    2. public GameObject Inhand;
    3.     public GameObject Itempickup;
    4.     public GameObject ShowKey;
    5.     private bool ShowKeybool = false;
    6.     private bool Isthere = true;
    7.     public bool pickedup = false;
    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.         if(pickedup == true)
    18.         {
    19.             Inhand.SetActive(true);
    20.  
    21.         }else
    22.         {
    23.             if(pickedup == false)
    24.             {
    25.                 Itempickup.SetActive(false);
    26.             }
    27.         }
    28.      
    29.         if(Isthere == true)
    30.         {
    31.             Itempickup.SetActive(true);
    32.         }
    33.         if(Itempickup == true)
    34.         {
    35.             ShowKey.SetActive(false);
    36.         }
    37.     }
    38.  
    39.     void OnCollisionEnter(Collision col)
    40.     {
    41.         Debug.Log("Successfully Collided!!");
    42.         Inhand.SetActive(true);
    43.         Itempickup.SetActive(false);
    44.         pickedup = true;
    45.      
    46.     }
    47.  
    48. }
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class SetActive : MonoBehaviour
    7. {
    8.     public GameObject SetActiveOnCollide;
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.  
    13.         SetActiveOnCollide.SetActive(false);
    14.  
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.  
    21.      
    22.  
    23.     }
    24.     void OnCollisionEnter(Collision col)
    25.     {
    26.         if(GameObject.Find("pickup").GetComponent<ActiveOnCollide>().pickedup == true)
    27.         {
    28.             SetActiveOnCollide.SetActive(true);
    29.         }else
    30.         {
    31.             SetActiveOnCollide.SetActive(false);
    32.         }
    33.     }
    34. }
    i dont know how to fix this really
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    It's a null reference error. And it tells you were. Within your OnCollisionEnter script. So, debug it. What could be null in that method?
    It's not SetActiveOnCollide, since you would get a null error in Start if it was.

    So, the next possible option is... most likely GameObject.Find is not finding your object. Start there.

    Null ref errors are some of the easiest errors to solve.
     
  3. devonmuhammad968

    devonmuhammad968

    Joined:
    Sep 30, 2018
    Posts:
    41
    thanks :)