Referencing non-static variable from script on child C#

I’m trying to reference a non-static varaible from a script, WeaponScript. WeaponScript is found in several child objects which are toggled on and off, and each contain a variable machineGun.bulletsLeft (I want to just change the enabled object’s variable).
Here’s what I have so far:

using UnityEngine;
using System.Collections;

public class ammopickup : MonoBehaviour {
	int ammo;
	Component script;

	void Awake () { 
		script = GameObject.FindWithTag("Player").GetComponentInChildren<WeaponScript>();
		ammo = script.machineGun.bulletsLeft;
		
	}
	
	void OnTriggerEnter(Collider other){
		ammo = ammo + 20;
		audio.Play ();
	}
}

This is the error I get:
WeaponSystem/ammopickup.cs(11,31): error CS1061: Type UnityEngine.Component' does not contain a definition for machineGun’ and no extension method machineGun' of type UnityEngine.Component’ could be found (are you missing a using directive or an assembly reference?)

How should I go about fixing this? Is it because the script can’t find the script to access the variable, or because I’m stupid and wrote the whole thing wrong. Thanks!

You have declared script like this:

Component script;

Meaning that you only have access to the variables and methods from the Component class. Now, it’s still a WeaponScript, but the compiler doesn’t know this. When you do this:

script = GameObject.FindWithTag("Player").GetComponentInChildren<WeaponScript>();

It works, because your WeaponScript is also a component (because it inherits from MonoBehaviour, which inherits from Behaviour, which inherits from Component), but on the next line, the compiler just sees that you’re trying to call .machinegun on a component.

To fix this, just declare the script variable as a WeaponScript:

WeaponScript script;

void Awake () { 
    script = GameObject.FindWithTag("Player").GetComponentInChildren<WeaponScript>();
    ammo = script.machineGun.bulletsLeft;
}

Note that “script” is a horrible variable name, since everything is a script.