Serialized class, Instances and access

I’m trying to implement integrated weapons controller and a simple inventory.

In the MainCharacterController.cs I’ve Got:

public class Inventory
  public int ammo1 = 100;
  public int ammo2 = 50;

  public int withdrawAmmo(string _type, _qnt)
    //do stuff and return qnt

public class MainCharacterController : MonoBehaviour
  //among other things....
  public Inventory _inv;

Inside **WeaponController.cs** Ive got:

public class WeaponController : MonoBehaviour
  MainCharacterController _main;
  Inventory _inv;

  void Start()
    _main = transform.GetComponent<MainCharacterController>();
    _inv = _main._inv;

and the _inv from weaponcontroller is giving nullReferenceException.

From what i’ve been thinking it must be about intance of this serialized class serving as inventory.

If anyone knows how to solve it ou a better way to implement this, help me please!


So, if your transform doesn’t find MainCharacterController, try this.GetComponentInChildren().
That would fix your problem.

Good luck.