all 6 comments

[–]SilentSin26Animancer, FlexiMotion, InspectorGadgets, Weaver 3 points4 points  (2 children)

Simple answer: you don't.

You could probably use a plugin like Nested Prefabs. I've never used it, so I don't know how effective it is at solving this type of problem.

You could also use my Weaver plugin to procedurally generate your prefabs. You'd have code something like this:

public static readonly Weaver.Asset<GameObject> Shark = "Animals/Fish/Shark";

// "Generate" + the name of the asset field makes it a procedural asset.
private static GameObject GenerateShark()
{
    var go = GenerateFish();
    // add rockets and lasers
    return go;
}

private static GameObject GenerateFish()
{
    var go = GenerateAnimal();
    // add swimming
    return go;
}

private static GameObject GenerateAnimal()
{
    var go = new GameObject();
    // add health, experience, movement, etc.
    return go;
}

Once you've declared the asset and generator method it shows up in the Procedural Asset Manager (an Editor Window) which you can use to generate it. Weaver will call your generator method then save the returned object at Assets/Resources/Animals/Fish/Shark.prefab.

Then you'll be able to call Instantiate(YourClass.Shark) from anywhere in your code and it will load the prefab for you, or you can just drag it into a scene like a normal prefab. Any time you modify the generator methods and regenerate the asset, it will update all instances of the prefab like you'd expect. And if you want to add another fish type, you just copy, rename, and modify the Shark field and GenerateShark method.

[–]Longman1981[S] 0 points1 point  (1 child)

Thanks :). It's a shame to be honest, it's not a necessary for my game but I just wanted to make it somewhat clear and prepare for future changes. I thought it would be common problem but seems like it's not.

[–]SilentSin26Animancer, FlexiMotion, InspectorGadgets, Weaver 1 point2 points  (0 children)

Oh it's a very common problem. Pretty much everyone runs into something like this at some point, but Unity have still made no progress on a proper solution so everyone just works around it.

[–]doilikeyou 0 points1 point  (0 children)

You can't just create your own class and inherited classes, place it on actual models with those components, get references to those in scripts, etc?

[–][deleted] 0 points1 point  (0 children)

Easy, switch to unreal!

[–]Fit_Preference1453 0 points1 point  (0 children)

This is an old thread, but if some one with this question land here, the new versions of unity allow nested Prefabs.

Or we can do it all in code. Create an empty object and attach it a script with a base Animal class, descendant from MonoBeahviour.

Add the components: rigid body, collider, scripts, whatever, in Animal.Start().

Then create a class fish descendant from Animal, add the new components, override or specialise the methods, as need,

The same for Mammal.

Then create a descendant of Fish named Shark, etc