Hi!
I have a serious issue here.
Let’s say I want to have a gameObject Tree and gameObject Bird.
I want to Bird have Components like: Fly, Walk, Shoot
and Tree: Grow, Fly (yes, my tree will fly
)
I know that components Fly and Walk will have to use some speed. It is pointless to declare in both Walk and Fly “float speed” and set it in inspector, so I make one class which does not inherits after MonoBehaviour and looks sth like that:
Class CharacterModel{
public float Speed;
public float Power;
}
Then , in order to have this CharacterModel avilalble in inspector and for other components I create :
CharacterInitializer{
public CharacterModel Character;
}
Now I can set in my model Speed and in each components where I need this value I just do
getComponent<CharacterModel>().Character //.Speed for example
It works just fine for a gameObject which is a bird like eagle or parrot.
But if I have a tree, I wouldnt use CharacterModel, I would rather create :
public Class TreeModel{
public float LeafsCount;
public float Height;
public float OxygenGiven;
public float Speed;
}
And now I certainly must create
public Class TreeInitializer{
public TreeModel Tree;
}
So If I want my components Fly and Walk use same Speed variable(‘field’) then I simply go for getComponent<CharacterInitializer>().Character.Speed;
But let’s say my game isn’t really realistic and my Tree also can fly.
I attach Fly component to my tree. And… It is not gonna work, because this tree has different Model getComponent<TreeInitializer>().Tree and Fly component need to have getComponent<CharacterInitiliazer>().Character.
So it looks like I would have to create seperate component like FlyTree which uses ().Tree.Speed, but compnents shouldnt behave like that, one Fly should work for each gameObject same way.
The question is: How to solve this issue? How to inteligently create components in this scenario?
Thanks for help in advance.
Looks like you just need another inheritance level, like so (psudo code): MovableClass: Monobehavior { Speed} Character: MovableClass { power} tree:MovableClass { leafcount, height, oxygen} }
– Glurth