I want to add a Component to a GameObject using an existing instance of the Component class.
Let's say I have the class Foo
:
public class Foo : MonoBehaviour {
int id {get; set;}
string name {get; set;}
//and other many variables
}
In another class I already have an instance of Foo
with all variables setup.
What I want to do is attach the existing instance of Foo
to a GameObject
, instead to attach it and then setup all the variables.
Something like that, it is just an example, of course it give errors:
GameObject go; //Existing GameObject
Foo foo; //Existing instance of Foo with variables setup.
go.AddComponent(foo);
Instead of doing:
GameObject go; //Existing GameObject
Foo foo; //Existing instance of Foo with variables setup.
go.AddComponent("Foo");
Foo myComponent = go.GetComponent<Foo>();
myComponent.id = foo.id;
myComponent.name = foo.name;
//and so on
Is there a way to do that?