- Home /
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;
}
}
}
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.
For a particle system you would most probably need a deep copy rather than a shallow one. There are multiple parts included.
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.
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);
}
}
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.
Ahh I wasn't thinking right. You are correct Sir. This won't compile for a Standalone Build.
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)
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.
Your answer
Follow this Question
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