Getting the value of a variable from script B to script A

Is it possible to get the value of the variable from script B to script A? I want to print the remaining enemies on the scene using OnGUI() but my raycast and GameObject[] enemies is in another script.

Here’s my code:

public class Damage: MonoBehaviour

GameObject[] enemies;

enemies = GameObject.FindGameObjectsWithTag ("enemy") as GameObject[];

RaycastHit hit;

if(Physics.Raycast(transform.position, transform.forward, out hit, 50f)){
      GameObject g = hit.collider.gameObject; 
      if (Vector3.Distance (transform.position, g.transform.position) < 10){
            Destroy(g);
      }
}
Here's the other script:

    public class GameStart: MonoBehaviour
    
    public GameObject player;
    public bool startGame, _player;
    
    public void Start(){
            Instantiate(player, transform.position, transform.rotation);
        }
    
    void OnGUI(){
            if (!startGame){
                GUI.Box(new Rect(mmPosX, mmPosY, 150, 150), "Main Menu 
 Controls: 
 
 W/S - forward/backward 
 A/D - rotate left/right 
 SPACE - bomb");
                if (GUI.Button(new Rect(sPosX, sPosY, 100, 30), "Start")){
                    startGame = true;
                }
            }
            else{
                GUI.Label(new Rect(10, 10, 100, 30), "Enemies Left: " + enemies.length); //call 'enemies' variable from 'Damage' script
            }
        }

            

assuming that your Damage script is on a gameObject with tag rayGameObject … then type the below code in your GameStart script …

Damage theScript;
uint remainingEnemies;

void Start(){
theScript = GameObject.FindObjectWithTag("rayGameObject").GetComponent<Damage>();
remainingEnemies = theScript.enemies.length;
}

this is an example to show how to access another script variables, now as you have access to script just use its public variables in the way you want …

Let you want to access a variable from A inside B…
Just make the variable declared as: protected static float position;
then change B like this:
class A:B ( i.e) inherit class B from A then you can easily access variable of A in B. Hope that clears your problem