Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Ochreous · Feb 19, 2014 at 02:31 AM · c#gameobjectcomponent

C# Adding Components From Other Gameobjects

I want to take a component's values and settings from one gameobject (in this case a particleSystem) and assign them to another Gameobject's component's values and settings. But I'm getting errors from my code. Is there something I'm doing wrong?

 using UnityEngine;
 using System.Collections;
 
 public class AddFromOtherGameobject: MonoBehaviour {
     public GameObject gameobject1;
     public GameObject gameobject2;
     // Use this for initialization
     void Start () {
     gameobject1.AddComponent("particleSystem");
             gameobject1.particleSystem = gameobject2.particleSystem;
             
           }
     }
 }
Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by vexe · Feb 19, 2014 at 04:59 AM

I have answered this before here. 2 answers, one for runtime, the other for the editor. Please google first.

Comment
Add comment · Show 3 · 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 clunk47 · Feb 19, 2014 at 05:07 AM 1
Share

Nice.

avatar image whydoidoit · Feb 19, 2014 at 06:42 AM 1
Share

For a particle system you would most probably need a deep copy rather than a shallow one. There are multiple parts included.

avatar image gwelkind · Jul 02, 2021 at 05:10 PM 0
Share

Thanks for answering and I know this is old. It would be helpful to link to those answers for those of us finding this answer on Google.

As is, I'm not sure how to find the actual solution you provided elsewhere, so I'm not sure this should be rated the most helpful answer.

avatar image
3

Answer by clunk47 · Feb 19, 2014 at 04:46 AM

If I'm understanding the question, you have 2 objects with the same script attached, and you want to match all the property values on both objects? What I did to test this out (C#), I made a simple scene with a camera and two cubes. On each Cube, I attach Example.cs.

 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour 
 {
     public float float1;
     public float float2;
 }

Now both cubes will have the example script with two properties. float1, and float2. Next, I wrote MatchProperties.cs, which uses UnityEditorInternal.ComponentUtility to copy component values from "go1", and paste the values into the Example component on "go2" after 3 seconds. I did a simple GUILayout Label to show the change happen. Here is MatchProperties.cs, which I attached to the Main Camera.

 using UnityEngine;
 using System.Collections;
 using UnityEditorInternal;
 
 public class MatchProperties : MonoBehaviour 
 {
     public GameObject go1;
     public GameObject go2;
     string label;
     
     IEnumerator Start()
     {
         go1.GetComponent<Example>().float1 = 10;
         go1.GetComponent<Example>().float2 = 20;
         go2.GetComponent<Example>().float1 = 0;
         go2.GetComponent<Example>().float2 = 0;
         yield return new WaitForSeconds(3);
         ComponentUtility.CopyComponent(go1.GetComponent<Example>());
         ComponentUtility.PasteComponentValues(go2.GetComponent<Example>());
     }
     
     void Update()
     {
         label = "go1 - float1 = " + go1.GetComponent<Example>().float1 + " \n" + "go1 - float2 = " + go1.GetComponent<Example>().float2 + "\n"
             + "go2 - float1 = " + go2.GetComponent<Example>().float1  + "\n" + "go2 - float2 = " + go2.GetComponent<Example>().float2 + "\n";
     }
     
     void OnGUI()
     {
         GUILayout.Label(label);
     }    
 }
 
 
Comment
Add comment · Show 4 · 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 whydoidoit · Feb 19, 2014 at 04:52 AM 2
Share

Good point - if it's in the editor that works. I'm thinking that isn't the OP's scenario though - looks like it is required at run-time.

avatar image clunk47 · Feb 19, 2014 at 04:54 AM 1
Share

Ahh I wasn't thinking right. You are correct Sir. This won't compile for a Standalone Build.

avatar image clunk47 · Feb 19, 2014 at 05:06 AM 1
Share

Dammit Vexe, you're surpassing me too quickly! XD

avatar image vexe · Feb 19, 2014 at 05:08 AM 2
Share

Just to make this clear, this does work at runtime - just the editor runtime (i.e. playmode) - but not the build's runtime (i.e. if you try to ship a game using this, you won't be able to even get a successful build cause this stuff is from the UnityEditor.dll which isn't included in the build)

avatar image
2

Answer by whydoidoit · Feb 19, 2014 at 02:39 AM

Yep you can't do that. A behaviour is associated with only one game object. You would need to copy the settings from one to the other. Unity Serializer can do that with it's SerializeForDeserializeInto and DeserializeInto methods.

Comment
Add comment · 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

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

23 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

Related Questions

C# Raycast 2D GameObject follow Mouse 1 Answer

Destroy + Score 1 Answer

Classes and Scripting 2 Answers

C# Rotate GameObjects Regardless of List Size 2 Answers

C# Change Script Help 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