C# battery - getcomponent

Hello, I’m creating a horror game and I got this flashlight that has limited power and I was just wondering how I would do if I want to create a battery to that flash light. I’ve been trying to do this myself for 1 ½ days but I havnt gotten it to work.

This is my batteryscript I’m trying to write to my flashligt

public class BatteryScript : MonoBehaviour 
{
	public float batteryPower = 20000.0f;
	private FlashlightController flashlightscript;
	public Transform Battery;
	private bool jagsson;


	// Use this for initialization
	void Start () 
	{
		jagsson = false;
	}
	
	// Update is called once per frame
	void Update () 
	{
		FlashlightController flashlightscript = GetComponent<FlashlightController> ();
		if (flashlightscript.powerLevel < 20000.0f && jagsson == true) //ERROR on this line.
		{
			flashlightscript.powerLevel = batteryPower;	//The powerLevel is the powerlevel of the flashlight which is 20 0000
		}
	}
	void OnTriggerEnter(Collider other)
	{
		jagsson = true; //I was just trying out stuff on this line here...
	}
}

Im trying to add this script to an object (The battery which I want to walk over and pick up and then my flashlight goes to full power) but Im getting this error and I got no idea how to solve it as I’m quite new to C#.

ERROR: Object reference not set to an instance of an object.

I know there are many questions about flashlights and battery pickups, and I’ve been searching alot about it but I havnt understood almost anything of it that’s why I’m here asking you guys.

I think you searched for the wrong thing. You should have searched for “Object reference not set to an instance of an object” to get better results. It’s a common problem, especially for newcomers, where you are trying to access an object that is null.

If you place this script on your battery, this script will call GetComponent() to retrieve the FlashlightController script from your battery game object. It doesn’t have one, so the value returned for flashlightscript is null.

You should rethink the logic here. If a battery game object is lying around in the environment and running an Update() call every frame, it would be doing this logic. You have the start of the idea with the OnTriggerEnter. You don’t need an Update() call at all. Just place the code in the OnTriggerEnter() method instead, and use the “Collider other” to access the flashlight, if possible. So the battery will collide with the character probably, so as long as the flashlight script is attached to the character, you could do it like this:

void OnTriggerEnter(Collider other)
{
  if (other.tag == "MyCharacter")
  {
    FlashlightController controller = other.GetComponentInChildren<FlashlightController>();
    if (controller != null)
    {
      // Do whatever with controller, recharge it, etc.
    }
    else
      Debug.Log("Uh oh, no flashlight on this guy!");
  }
}

I added a check for the tag, because what happens if you swing a sword through the battery, if it has a collider, it will enter this code. Of course you don’t have to have the tag if you just ignore the (controller == null) case.

For future posts, please indicate exactly which line the code is throwing the error on in the text body. I didn’t see the comment in the code until after I scrolled to the right.