• 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 /
avatar image
0
Question by kokololo911 · May 20, 2013 at 07:41 PM · gameobjectplayer

Adding component to component

Hi, i am new to component object models. Here is what i want to ac$$anonymous$$eve.

I have an actor component for my game objects. The actor component as a private field of type behavior. I need to be able to extend the behavior class and add it to the actor. Like t$$anonymous$$s.

alt text

seri.png (38.6 kB)
Comment
Add comment · Show 3
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 Benproductions1 · May 21, 2013 at 12:14 AM 1
Share

Why not just add it as another component and access it through GetCommponent?

avatar image whydoidoit · May 21, 2013 at 03:42 AM 0
Share

The reason you can't just drag it on there is that the variable in Actor requires an instance of the script (not just a reference to the script). So if you add the PlayerBehaviour to the GameObject as Benproductions suggests you can get it through GetComponent. Any object with that script added could be dragged and dropped onto that variable in the inspector.

avatar image kokololo911 · May 21, 2013 at 04:41 PM 0
Share

Can i make the Behavior field be a type and then instantiate from the type ?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MikeNewall · May 21, 2013 at 03:28 AM

Do you want to switch between behaviors at runtime? Declare your behavior field like you have already. When you want to add a different behavior simply destroy the old one and add the new:

 Destroy(behavior);
 behaviour = gameObject.AddComponent(typeof(Attack));
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 kokololo911 · May 21, 2013 at 03:48 PM 0
Share

Yes i would need to be able to modify them at run time. I cannot instantiate Attack since its not a component but a scriptableObject.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I stop an immediate collision with all objects from ocuring at the entry of game mode? 0 Answers

Destruction Help!!!!! 1 Answer

How to make a box (or any object) appear on collide? 1 Answer

Coding Gui Text To Perform After GameObject Destroy? 1 Answer

Player 1 and Player 2 move together 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