error CS0122: 'Flashlight.AlterEnergy(int)' is inaccessible due to

I Have an error that is holding my game development back error
Assets\Scripts\Battery.cs(46,9): error CS0122: ‘Flashlight.AlterEnergy(int)’ is inaccessible due to its protection level

. Here is the code :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Battery : MonoBehaviour
{
    public Flashlight fL;
    bool buttonInRange;
    bool buttonActivated;
    AudioClip batterySound;
    private static float batteryPower = 10;

    public GUISkin guiSkin;

    private bool hasPlayed = false;

    void OnTriggerEnter(Collider c)
    {
        buttonInRange = true;

    }
    void OnTriggerExit(Collider c)
    {
        buttonInRange = false;

    }
    void OnGUI()
    {
        if (buttonInRange == true)
        {
            GUI.skin = guiSkin;
            GUI.Label(new Rect(Screen.width / 2 - 50, Screen.height / 2 - 55, 120, 50), "Pick up batteries");

        }

    }
    void Update()
    {
        if (buttonInRange == true)
        {
            if (Input.GetKeyDown("e"))
            {
                if (!hasPlayed)
                {
                    AudioSource.PlayClipAtPoint(batterySound, transform.position);
                    fL.AlterEnergy(batteryPower);
                    Destroy(gameObject);
                    hasPlayed = true;

                }

            }

        }

    }
}

This mechanism is the root cause of your problem:

Remember: The complete error message contains everything you need to know to fix the error yourself.

Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors.

The important parts of the error message are:

  • the description of the error itself (google this; you are NEVER the first one!)
  • the file it occurred in (critical!)
  • the line number and character position (the two numbers in parentheses)
  • also possibly useful is the stack trace (all the lines of text in the lower console window)

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.

Beyond that, NOBODY memorizes error codes. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.