Hey everyone. Thanks for reading my question. I am trying to have one script (the gun) refer to another script (the ammo) for some variables but I keep getting an error (CS0236).

Here is the ammo script (ammo.cs)
using UnityEngine;
using System.Collections;

public class ammo : MonoBehaviour {

	public float ammoInClip = 15f;
	public float clipCapacity = 15f;
	public float clips = 5;
}

and here is the gun script (AK47.cs)

using UnityEngine;
using System.Collections;

public class AK47 : MonoBehaviour {
	
	public GameObject bullet_prefab;
	public float bulletImpulse = 20f;
	public float cooldown = 0.1f;
	float coolDownRemaining = 0; 
	public int ammoInClip;
	public int clipCapacity;
	public int clips;
	private ammo _ammo = GetComponentInChildren<ammo>();
	
	// Update is called once per frame
	void Update () {
		ammoInClip = _ammo.ammoInClip;
		clipCapacity = _ammo.clipCapacity;
		clips = _ammo.clips;

		coolDownRemaining -= Time.deltaTime;
		
		if( Input.GetMouseButton(0) && coolDownRemaining <= 0 && ammoInClip >= 0) {
			coolDownRemaining = cooldown;
			Camera cam = Camera.main;
			GameObject thebullet = (GameObject)Instantiate(bullet_prefab, cam.transform.position + cam.transform.forward, cam.transform.rotation);
			thebullet.rigidbody.AddForce( cam.transform.forward * bulletImpulse, ForceMode.Impulse);
			ammoInClip -= 1;
		}
		if (Input.GetMouseButton (0) && ammo.ammoInClip <= 0 && ammo.clips >= 1) {
			Debug.Log("Reloaded", gameObject);
			clips -= 1;
			ammoInClip += (clipCapacity);
		}
	}
}

It has a problem with line 13: Assets/Scripts/Guns/AK47.cs(13,30): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.GetComponentInChildren(System.Type)’

If anyone could help, I would really appreciate it. I’m sure there’s gotta be someone else out there trying to do the same thing.

Thanks again!

You have several problems here. First you can not call GetComponentInchildren in a field initializer. You have to call it in Start or Awake:

[...]
private ammo _ammo;

void Start()
{
    _ammo = GetComponentInChildren<ammo>()
}

Next thing is, at the moment you copy the values of your variables into local variables each frame. That makes no sense and in addition changes made to local variables are lost since you overwrite the values each frame. You should remove the local variables and use _ammo.XXXX instead.

Not a real problem but a violation of coding standards: class names have to start with a capital letter to distinguish them from variable names.

Thanks everyone for your help! I got the code working as I hoped it would. Here is what it looks like now:

ammo.cs

using UnityEngine;
using System.Collections;

public class ammo : MonoBehaviour {

	public float ammoInClip = 15f;
	public float clipCapacity = 15f;
	public float clips = 5;
}

AK47.cs

using UnityEngine;
using System.Collections;

public class AK47 : MonoBehaviour {
	
	public GameObject bullet_prefab;
	public float bulletImpulse = 20f;
	public float cooldown = 0.1f;
	float coolDownRemaining = 0; 
	private float ammoInClip;
	private float clipCapacity;
	private float clips;
	private ammo _ammo;
	
	// Update is called once per frame

	void Start(){
		_ammo = GetComponentInChildren<ammo> ();
	}
	void Update () {
		coolDownRemaining -= Time.deltaTime;
		
		if( Input.GetMouseButton(0) && coolDownRemaining <= 0 && _ammo.ammoInClip >= 0) {
			coolDownRemaining = cooldown;
			Camera cam = Camera.main;
			GameObject thebullet = (GameObject)Instantiate(bullet_prefab, cam.transform.position + cam.transform.forward, cam.transform.rotation);
			thebullet.rigidbody.AddForce( cam.transform.forward * bulletImpulse, ForceMode.Impulse);
			_ammo.ammoInClip -= 1;
		}
		if (Input.GetMouseButton (0) && _ammo.ammoInClip <= 0 && _ammo.clips >= 1) {
			Debug.Log("Reloaded", gameObject);
			_ammo.clips -= 1;
			_ammo.ammoInClip += (_ammo.clipCapacity);
		}
	}
}

If anyone has any other suggestions, I’m all ears. Thanks again!

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class shooter_with_mag : MonoBehaviour {	
	public Rigidbody projectile;
	public float speed = 20;
	public float charge;
	public float magazine;
	public Text Textcharge;

	void Update () {
		//charge += 1;
		if(charge == 200)
		{
			magazine++;
			charge -= 100;
		}
		if((charge == 0) && (magazine >=1))
		{
			magazine--;
			charge += 100;
		}
	
		Textcharge.text = magazine.ToString () + "/" + charge.ToString ();
		if (Input.GetMouseButtonDown(0) && (charge >= 100) && (magazine >=0 ))
		{
			Rigidbody instantiatedProjectile = Instantiate(projectile,
			                                               transform.position,
			                                               transform.rotation)
				as Rigidbody;
			instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));
			/*///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
			if((charge >= 100) && (magazine >= 0))
			{
				charge -= 100;
			}
		}
	}
}