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!