Object reference not set to an instance of an object (Cannot Loot Object)

Hello i have 2 scripts one is for Flashlight “Battery UI script” Storing how many batteries i have left
for reload a flashlight max 5 that script is working totally fine but when i try to loot a battery it occur a error
and i dont understand why because i setup input “Use” in Project Settings correctly because another key “Grab” is working fine on Key “Q” but “Use” on Key “F” not working and i getting this error what i do wrong?!

i will paste code bellow with screenshots as well
in this line of code i have error

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class BatteryPickup : MonoBehaviour {
    private Transform myTransform; 
    private GameObject MessageLabel;
    private GameObject BatteryUIScript;
    public bool EnableMessageMax = true;

    public bool Enabled;
    public float BatteryAdd = 0.01f;   

    public AudioClip pickupSound;//sound to playe when picking up this item

    public string MaxBatteryText = "You have Max Batteries";
    public Color MaxBatteryTextColor = Color.white;

    public bool PickupMessage;
    public string PickupTEXT = "Battery +1";
    public Color PickupTextColor = Color.white;

    void Start () {
        myTransform = transform;//manually set transform for efficiency
    }




    public void UseObject (){
    BatteryUIScript = GameObject.Find("Flashlight");
    BatteryUI BatteryComponent = BatteryUIScript.GetComponent<BatteryUI>();

        if (BatteryComponent.EnableBattery == true)
        {
        Enabled = true;
        }

    if(BatteryComponent.EnableBattery == false){
        Enabled = false;
        if(EnableMessageMax){StartCoroutine(MaxBatteries());}
    }

    if(Enabled){
        StartCoroutine(SendMessage());
        BatteryComponent.Batteries += BatteryAdd;
        if(pickupSound){AudioSource.PlayClipAtPoint(pickupSound, myTransform.position, 0.75f);}
        this.GetComponent<Renderer>().enabled = false;
        this.GetComponent<Collider>().enabled = false;
    }
  }

    public IEnumerator SendMessage (){
        MessageLabel = GameObject.Find("UI_MessageLabel");
        Text Message = MessageLabel.GetComponent<Text>();
        /* Message Line */
        EnableMessageMax = false;
        Message.enabled = true;
        Message.color = PickupTextColor;
        Message.text = PickupTEXT;
        yield return new WaitForSeconds(2);
        Message.enabled = false;
        EnableMessageMax = true;
    }

    public IEnumerator MaxBatteries (){
        MessageLabel = GameObject.Find("UI_MessageLabel");
        Text Message = MessageLabel.GetComponent<Text>();
        /* Message Line */
        if(!Enabled){
            EnableMessageMax = false;
            Message.enabled = true;
            Message.color = MaxBatteryTextColor;
            Message.text = MaxBatteryText;
            yield return new WaitForSeconds(3);
            Message.CrossFadeAlpha(0f, 2.0f, false);
            yield return new WaitForSeconds(4);
            Message.enabled = false;
            EnableMessageMax = true;
        }
    }
}

What error?

Oh, you put it in the subject line.

Right, so if that’s the line you get that error on, it means that it found a GameObject named “Flashlight”, but there was no BatteryUI script attached to it. I’m guessing that you have multiple objects named “Flashlight” (one in the UI, one in the scene?) and it found the wrong one.

This is one of the big reasons why GameObject.Find is terrible and should always be avoided.

i find the error

public void UseObject (){
BatteryUIScript = GameObject.Find(“Flashlight”);
BatteryUI BatteryComponent = BatteryUIScript.GetComponent();

i have Flashlight object and in it is his children spotlight and on that spotlight i have attached script and is not working
pickuping battery but if i attach it to the flashlight then pick up works but reloading battery doesn’t work so
i need to change getcomponent to a children right?

GetComponentInChildren will get you what you’re looking for.

But still read that article about GameObject.Find