- Home /
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!
script = GameObject.FindWithTag("Player").GetComponentInChildren<WeaponScript>();
will get any WeaponScript
on "Player".
if you're trying to get a specific weapon then you'll need to be more specific in how you get it!
why not have your machine gun inherit from WeaponScript
to allow you to have common functionality, but access machine gun specific variables/functionality where required?
that way you can find $$anonymous$$achineGunScript
(to use your nomenclature) ins$$anonymous$$d of any weapon.
Answer by Baste · Feb 11, 2015 at 09:44 AM
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.
That did the job! Thanks (and I know script is a band variable name, I just did it for the question haha)
Your answer

Follow this Question
Related Questions
An object reference is required to access non-static member 1 Answer
Get variable from seperate script. 1 Answer
reference is required to access non-static member `CarController.Update()' 1 Answer
An object reference is required to access non-static member 4 Answers
Reference creator of gameObject 1 Answer