Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

At runtime I want to copy one Gameobject component to another gameobject. In my case I have one camera where multiple scripts are added with values settings.

the same components i want to add into my antoher camera at runtime i have tried this so far, Getting all compoenent of an object then tryting to add but it is not working.

Component[] components = GameObject.Find("CamFly").GetComponents(typeof(Component));
            for(var i = 0; i < components.Length; i++)
            {
               objCamera.AddComponent<components[i]>();
///error in above line said adds a component class named/calss name to the gameobject

            }
share|improve this question
2  
you really need to stop duplicating your questions here and on stack overflow. – Uri Popov Jun 28 at 8:50
    
@UriPopov If you see cross posts, flag for moderator attention and link to the SO question in the flag message! – Alexandre Vaillancourt Jun 29 at 12:28

Well, AddComponent's generic parameter expects a type, not a string. AddComponent actually creates an entirely new component of the given type. As far as I know there is no way to get unity to add a new component AND copy the values from another.

However, what you seem to be doing is copying ALL the components of one game object to another. If appropriate, I'd recommend just using GameObject.Instantiate, which creates a copy of an entire game object, including all it's components.

If you really need to copy components without creating a new game object, there's an answer here:

http://answers.unity3d.com/questions/458207/copy-a-component-at-runtime.html

share|improve this answer

The main issue here is that you're trying to call AddComponent with an object rather than a type, which is what's causing your issue. There are 3 possible ways you can fix this:

Solution 1
Use AddComponent with a generic function

This is very close to what you already have, and only requires you to use GetType rather than the object itself

Component[] components = GameObject.Find("CamFly").GetComponents(typeof(Component));
for (var i = 0; i < components.Length; i++)
{
    objCamera.AddComponent<components[i].GetType()>();
}

Solution 2
Use AddComponent with a type as a parameter

Very similar to the first solution, but uses GetType as a parameter. If you were using the return value, then you'd notice that the type returned here is Component, whereas the solution aboves return type is whatever you pass in to the generic function

Component[] components = GameObject.Find("CamFly").GetComponents(typeof(Component));
for (var i = 0; i < components.Length; i++)
{
    objCamera.AddComponent(components[i].GetType());
}

Solution 3
Use AddComponent with a string argument

Again, this relies on GetType, but uses a string instead of the type object. I haven't tested this, and my gut says it will be the slowest solution (because Unity will need use reflection to find the type you want), but is here so you can see the possible uses

Component[] components = GameObject.Find("CamFly").GetComponents(typeof(Component));
for (var i = 0; i < components.Length; i++)
{
    objCamera.AddComponent(components[i].GetType().ToString());
}
share|improve this answer
    
all three solution i have tried all is vain – Mohammad Faizan Khan Jun 28 at 9:01
    
Do you get the same error for each? Or do you get different errors/results? – hobnob Jun 28 at 9:10
    
Also, for copying values over, I'd definitely recomend GameObject.Instantiate as @Ben in their answer – hobnob Jun 28 at 9:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.