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!

  • Jonathan Gonzalez(jgonzalez) replied

    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

  • Will (williama) replied

    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.

  • Will (williama) replied

    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;

        }

    }

  • Hunter & Tiff Eidmann(eithman) replied


    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? 

  • Jonathan Gonzalez(jgonzalez) replied

    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. 

  • Hunter & Tiff Eidmann(eithman) replied

    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. 

  • Jonathan Gonzalez(jgonzalez) replied

    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.

  • Hunter & Tiff Eidmann(eithman) replied


    jgonzalez  I have watched several tutorials on classes and I believe I have most of the basics down. But I am confused on how often to use them. The one video I watched made it seem like you should use a class for every specific action you want your gameobject to do but I thought this was the purpose of methods. For example, I have a block of code in my main script that makes the animal walk around, another that seeks out food, another that decides whether the animal has a baby or not, etc. This main script is pretty long, dense and hard to read so I thought maybe I should take all those chunks of code and turn then into classes but I am not exactly sure what is the deciding factor when choosing between a method and a class. Any suggestions?

  • Jonathan Gonzalez(jgonzalez) replied

    eithman I would recommend reading this article: https://unity3d.college/2017/01/10/unity3d-architecture-srp/

    In essence a class should be responsible for something very specific. If you create one generic class that can be used to determine how the animals should walk around, that should be a specific class. You can then reuse that with other similar animals. If you had that same class but had something very specific in it to one animal, you now have to create a whole new class for another similar animal. 

    Think of classes as addons. When you're creating a character in Unity you'll probably add an Animator for animations, a rigidbody for mass and gravity, an audio source for playing audio. Each of these is a class but they don't need to work with each other. Build your classes in a similar way. Make it easy to add/remove these features by making your classes do something specific.

    In your case you might have a class for what the animal is. Another for that animals health. Another class could be use for general patrolling and looking for food. When you write very long classes it makes it harder to manage and you're actually making it harder to customize things. Same is true for methods, make them easy to organize and use. The more a class/method depends on something the more difficult it is to organize and manage.