0

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

" Get Error in this line (see screenshot) "

Click for code screenshot error

battery GUI on top right corner 3/5 collected batteries

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;
        }
    }
}
Mikael
  • 1,002
  • 1
  • 11
  • 22
BraniDev
  • 13
  • 1
  • 4
  • Apparently `BatteryUIScript.GetComponent()` returns `null`. Is there such component on that object? – derHugo May 29 '20 at 15:50
  • i have "BatteryUI" script attached to the flashlight and is working normally oh could be i need renamed it to "BatteryUIScript" ??? maybe that's the case? – BraniDev May 29 '20 at 16:07
  • 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? – BraniDev May 29 '20 at 18:40
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – BugFinder May 30 '20 at 00:59

0 Answers0