trying to invoke a void in a sub sub class

hey i am trying to invoke a void from a sub class in a sub class but if i try to add monobehaviour it just breaks my script

public class GatherGlobal : MonoBehaviour {

    public Resources ResourceMenu;




    //THIS IS ALL RESOURCE'S AND HOW MUCH AND BUILDING UNDER THAT CATAGORY
    [System.Serializable]
    public class Resources
    {


        public Wood WoodResource;
        public Meat MeatResource;
        public Stone StoneResource;
        public metal MetalResource;
        public Gold GoldResource;



        [System.Serializable]
        public class Wood
        {

            public Cooldowns CD;
            public BuildButton[] BBwood;
            public ResourceManager RM;
            public float BasicAmount;

            //THIS IS THE COOLDOWNTIME AND SLIDER
            [System.Serializable]
            public class Cooldowns
            {
                //all cooldown value's
                public float CoolDown;
                [System.NonSerialized]
                public float CoolDownMax;
                public Slider CoolDownSlider;
            }
            //END COOLDOWN CLASS


            void GiveWood()
            {
                CD.CoolDown = CD.CoolDownMax;
                RM.Wood += BasicAmount;
                //wood resource adder
                foreach (BuildButton item in BBwood)
                {
                    item.Invoke("GiveResourcesOut", 0f);
                }
            }
        }


and this is what happpens when i try to add monobehaviour on “Wood Resource”

is there a way to invoke GiveWood without having monobehaviour or do i do something wrong that makes monobehaviour break the class?

Bump

Firstly class definitions inside of another class are not subclasses. They are nested classes and generally you shouldn’t use them (this is an over simplification, there are reasons to use them, but those reasons are much more advanced than what you are tackling here).

You need to rewrite your code to use separate classes (maybe this is done and this is just copy ‘n’ paste omissions, hard to tell).

I’d also consider having one resource type class instead of separate classes for wood, meat, etc. It looks like your resources are all the same, so simply defining a member to hold the type seems like a better approach (either a string or an enum). If you do have separate classes make sure they inherit from a common base class.

Your data classes (resources, wood, etc), shouldn’t be MonoBehaviours. MonoBehaviours are for components that are added to a GameObject.

You need to work on your terminology: “Invoke a void” doesn’t mean anything. You invoke a method, its return type (in this case void) has no bearing on it being accessible or not. Access modifiers like public and private define access for a member or method.

If a method is public it can be invoked from anywhere that has an instance of the owning class. You probably want your GiveWood() method to be public.


You really need to go back to basics and make sure you understand them. Occasionally diving in the deep end is good, but swimming out too far is only going to lead to drowning.

1 Like