Hunter & Tiff Eidmann

Is there something between a public variable and a static public variable?

I am wondering if there is some way I could make a variable that is shared like a static variable but only with a specific set of game objects. In my game I have animals that have babies. I am trying to figure out how to make variables that are shared by all the members of a family but not shared with any members of other families. Any help would be really appreciated!

  • crew

    Static variables are essentially global variables that any class can access (if it's public). Use these sparingly in cases where you need to have a shared variable between various objects. When you create a class with variables, each object has its own unique values. So a car made from the same vehicle class could have 5 different variations due to the changes in variable values. Static variables stay the same as it's part of the class and not the instance of the object created. 

     There are a few things you can do, but it depends how complex you want to get. You could just use public variables and have only those families access them as needed. That's the easiest but also not the most secure. 

    The next step would be to create specific classes for these families. For example you could create a base "dog" class, and any subsequent families that would fit within this criteria would derive from the dog parent class. 

    Variables within those classes would be accessible to classes but not others. You can of course make public variables within them to make them available to outside classes, but that's a general idea. It's a bit more complex doing that, but can usually mean closer integration and possibly more efficient coding. Here is a brief explanation of how inheritance works: https://unity3d.com/learn/tutorials/topics/scripting/inheritance

  • An option would be to create a Family class, and store the properties that you want the members to share in that. Then add a Family property to your animal class, and assign the specific Family instance to each of it's members.

    That way they will share the same Family details, but others won't be able to see it. Different family groups will each share their own Family instances, but won't be accessing the other families data.

    That might sound confusing. If you need more clarity on the meaning of 'instance' in this example, ask away.

  • Here's how that might look:


    public class Family 

    {

        // Add any shared properties here

        public string Name {get;set;}

    }

    public class Animal

    {

        // this could be public, or keep it more internal

        protected Family Family { get;set; }

        public Animal(Family fam)

        {

            Family = fam;

        }

        // you'll probably want to do somethng more useful than this, but it should

        // give you the idea

        public string FamilyName

        {

            get { return Family.Name; }

        }

    }

    public class SpecificAnimal : Animal

    {

        public SpecificAnimal(Family fam)

            : base(fam) // pass the family instance into the base class

        {

            // do any other setup here

        }

        public void DoSomething()

        {

            // we can access the Family properties from in this specific class too

            var name  = Family.Name;

        }

    }


  • jgonzalez Making a family class seems to be the way to go but I have a few more questions related to prefabs and inheritance. Let's say I start the game with just two animals. I do this by instantiating 2 copies from the same prefab at the center of the game map.  What makes each animal unique is that in the prefab some of  their attributes, like speed and fertility, are chosen randomly. So when I instantiate each of them, the random number generator immediately gives them each their own unique attributes. Later on when the animals are big enough they have children and these children are instantiated from the child prefab. 

    My first question is where to put the " Family" class?  Would I make it inside the the script of the prefab that the original two animals derive from? 

    Second, how do I make it so that each baby animal is inheriting the "Family" class of their specific parent animal and not the other animals "Family" class? 

  • crew

    eithman I can put together a small example later today if I have time. The gist of it is that you'd create the family base class and that could either reside on an object in your game or solely in your project panel. Then you'd create child classes that derive from that class. 

    Whenever you create a script in Unity it automatically derives from Monobehaviour which is the base class. It's not one you created but it allows you to utilize things from that base class. Same would be true for your custom classes. 

    So all of this would be done through the scripting aspects. The animals you'd instantiate would need to have the classes that you want. You would be creating that yourself and managing it. 

    So a quick example of this. Create a dog class. Add that to the main animal in the game. Then create a second class called Puppy that derives from the dog class. Those objects with the puppy class would be the objects you'd want instantiated from the object that has the dog class and no other. 

  • Do you have any recommendations on where I can get an in depth tutorial series on classes? I have looked through youtube but they mostly seem to be one off tutorials only going over the very basics. 

  • crew

    eithman All the ones I'm familiar with are quite basic. Going in depth with them usually means more specific examples. This link might be a good place to start: https://videos.raywenderlich.com/courses/47-beginning-c/lessons/24

    Inheritance is something I'll cover in the future, possibly add it to the Game Programming Bootcamp.