• 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
Question by benevolent · Dec 28, 2022 at 10:29 AM · gameobjectscript.monobehaviour

How to add a pre-existing MonoBehaviour component object (stored in a variable set at runtime) to an pre-existing game object

I would like to do this (example below). This is just an example (I am not in my game using the constructor to create the MonoBehaviour/GameObject, I am instead storing the MonoBehaviour in a variable and then want to add it in its current state as a component to another game object). \r\n

 // I have a MonoBehaviour component stored in a variable with some configuration on it
 var anim = new Animator();
 
 // Example configuration
 anim.applyRootMotion = true;
 
 // I have a GameObject I want to add the MonoBehaviour component inside the variable to  
 var obj = new GameObject();
 
 // I want to add this pre-existing component to the object, rather than adding a new one with AddComponent<Animator>(). This code does not work (Compiler Error CS1503)
 obj.AddComponent(anim);
 
 // This below is NOT what I want (the Animator will not have the properties on it like the one the variable has)
 obj.AddComponent<Animator>();

I would appreciate it if anyone knows a solution that does this behaviour specifically, rather than a workaround. I am not able to use a prefab to instantiate the object that has a component with the desired pre-configured state set in the editor. \r\n

It needs to be done at runtime (the setting of the component variable and the adding of the component). \r\n I did see this https://answers.unity.com/questions/923435/adding-components-from-prefab-into-existing-gameob.html but it's based on a prefab, rather than runtime scripting. And I don't really want to use reflection if at all possible.

If there is definitely no solution, then a workaround would be nice though :).

Thanks!

Comment

People who like this

0 Show 0
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

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by Sonky108 · Dec 28, 2022 at 10:35 AM

@benevolent

You can for example do something like:

var anim = obj.AddComponent<Animator>();

and then:

anim.applyRootMotion = true;

Comment
logicandchaos

People who like this

1 Show 5 · 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 benevolent · Dec 28, 2022 at 10:36 AM 0
Share

Thanks for the reply. The problem is I can't hard-code this property. It needs to be set from some other code.

For example, another script that sets the properties based on some logic, and then later on the script I want to set the component on the object is called.

avatar image Sonky108 benevolent · Dec 28, 2022 at 10:50 AM 0
Share

Oh, that's getting kinda tricky then. Doesn't matter if it's set from code or data is stored in ScriptableObject, at certain point you will need some kind of bridge. You can have a helper decorator class, let's call it AnimatorHelper. AnimatorHelpercontains method that takes Unity MonoBehaviour (or Animator in that case) and your data or definition of your data. That can be ScriptableObject, an interface or just a concrete implementation of how/what/when should be applied. Then from any other place having a reference to that AnimatorHelper you can call that method to apply/refresh those properties.

 public class AnimatorHelper
 {
     private Animator _animator;
 
     public AnimatorHelper (Animator animator)
     {
         _animator = animator;
     }
 
     public void Apply (IAnimatorRules animatorRules)
     {
          animatorRules.Apply(_animator);
     }
 
     public void Apply (AnimatorRulesSO animatorRules)
     {
          // translate your stored SO data to Animator
     }
 }
 
 
 public interface IAnimatorRules
 {
     public void Apply (Animator animator);
 }
 
 
 public class EnableRootMotion : IAnimatorRules
 {
     public void Apply (Animator animator)
     {
          animator.applyRootMotion = true;
     }
 }
 

avatar image Sonky108 Sonky108 · Dec 28, 2022 at 10:52 AM 0
Share

Then from some place in code you can use:

 _animatorHelper.Apply(new EnableRootMotion());
 _animatorHelper.Apply(new DisableRootMotion());

or any other more complex behaviour

Show more comments
avatar image benevolent · Dec 28, 2022 at 11:16 AM 0
Share

This looks great and object-oriented. Thanks for the all the details. I won't have a chance to test it fully right now, but I will get back to you soon. I am using SO's for this stuff so thanks for including that info too!

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

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

Do you tend to have scripts to mark prefabs 1 Answer

Classes : extend MonoBehaviour issues 1 Answer

How can I return a GameObject? 1 Answer

How to prevent a script on a disabled object from firing? 0 Answers

How to access a game object from a different scene? 0 Answers


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