What is the correct way to use prefab in my example?

So I’ve got a plant GameObject which has stuff like how long does it takes to grow, how does it look when growing and when grown (sprites) etc., as well as scripts to make it grow.


Until now, whenever I needed a new plant I just duplicated the prefab and changed things accordingly, but now I wanted to add a particle effect whenever a plant grew, and it made me rethink if I even use prefabs in the correct way, because I could add a particle system via script, but if that would be a single prefab I could just add it via inspector instead.


Is there a way that I can use a single prefab like some kind of class? I thought about one prefab called Plant, but I have no idea what is the best way to add new plants and set their values (as I mentioned, I have Sprites of the plants and it feels more comfortable to set it via inspector, not via code). I want it to be possible to have multiple plants of multiple types (for example I can have 3 carrots that behave exactly the same and 2 potatoes that behave like a potato, but not like a carrot).

You should use prefab variants to do what you are looking for.

Start with a base prefab, that holds all the stuff like audio, base scripts like growing and so on. Here you place your “grow” particle system as well.
Now you can create prefab variats of that base prefab that represent your different plants. For each plant a new one - if they have different functionality.

Like this you can still edit the base prefab and e.g. change the particle effect for growing and all your variants automatically get the changed particles without you having to change them all.

To be more precise:

  • create a base prefab “plant” of an empty gameObject
  • add the base scripts to this prefab
  • add particle effect to that prefab
  • now create a prefab variant off of that base prefab called carrot
  • add all components you need to implement the carrot on that new prefab variant like the sprites of the carrot and the special behaviour scripts it can have.

You can have a class that contains instances of itself, that you can then assign in the inspector, and display accordingly. For example:

    public class Plant
    {
        // These can all be assigned in the inspector
        // as long as you have a script containing this class
        // and you only need one prefab that can have
        // different assigned stages

        public Plant GrowingStage1; 
        public Plant GrowingStage2;
        public Plant GrowingStage3;
        public Plant Grown;

        public float TimeTakenToGrow = 60;

        //...
    }

@Hakej37 Is this what you wanted?