Share common data for all components in gameObject
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):
$$anonymous$$ovableClass: $$anonymous$$onobehavior { Speed}
Character: $$anonymous$$ovableClass { power}
tree:$$anonymous$$ovableClass { leafcount, height, oxygen} }
to expand a bit.. you could use this inheritance hierarchy like so:
useThisSpeedSomehow = getComponent<Character>().speed;
OR
useThisSpeedSomehow = getComponent<Tree>().speed;
OR
useThisSpeedSomehow = getComponent<$$anonymous$$ovableClass>().speed;
All these components will have a speed member. Character and Tree, inherhit speed from $$anonymous$$ovableClass.
Answer by Dave-Carlile · May 20, 2016 at 12:16 PM
Why not just make your fly behavior a Monobehaviour? Unity works by composition - you add the behavior you want by adding a component that implements that behavior. If you want your bird to fly then you add the Fly component. If you want your tree to fly then you add the Fly component to that object.
If both of those components need to share information about how to move then put that information into a third component - Moveable or something. Your Fly and Walk components can find that component via the GetComponent method and then access the values - e.g. Speed.
Hm, sounds like one of possible solutions, but I thought about it and wouldn't the best solution be to have component stats where I 'd have all stats for tree and for bird. If some value is never meant to be used then it would be simply null,0,""...? Thanks to that I would always know where to find needed properties. And in case I would need to save data, I would have class ready for that, since it doesnt inherits from mono.
Your answer
![](https://koobas.hobune.stream/wayback/20220612081201im_/https://answers.unity.com/themes/thub/images/avi.jpg)