Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by mwozniak93 · May 20, 2016 at 11:49 AM · c#modelscomponentsserializable

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Glurth · May 20, 2016 at 12:10 PM 0
Share

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} }

avatar image Glurth Glurth · May 20, 2016 at 11:02 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mwozniak93 · May 20, 2016 at 04:27 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

When re-parenting instantiated object as child of current gameObject, why does GetComponents() not retrieve instantiated object's components? 1 Answer

Disabling components on objects with tags 1 Answer

EndOfStreamException: Failed to read past end of stream. 0 Answers

How to store all components of a GameObject in an array? 0 Answers

No overload for method. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges