- Home /
GetComponent and AddComponent with variables for Collider type
Having a problem with using GetComponent and AddComponent in a C# editor script. I'm trying to destroy a Collider and then add a new Collider, by using variables for the Collider types... but Unity is throwing an error (same error for both Get and Add Component:
"error CS0246: The type or namespace name `prefabsCollider' could not be found. Are you missing a using directive or an assembly reference?"
Code:
Collider prefabsOldCollider = prefabOld.collider;
Collider prefabsNewCollider = prefabNew.collider;
DestroyImmediate(prefabToSet.GetComponent<prefabsOldCollider>());
prefabToSet.AddComponent<prefabsNewCollider>();
Please help,
Thanks!
Answer by Bunny83 · Jul 04, 2011 at 01:30 PM
The generics parameter have to be a type, not a variable. To do what you want you have to use the System.Type version of AddComponent();
System.Type newColliderType = prefabNew.collider.GetType(); // Get the type object for the new collider
DestroyImmediate(prefabToSet.collider); // Remove the old collider, the type doesn't matter
AddComponent(newColliderType); // Add a new collider of this type.
Can be simplified:
DestroyImmediate(prefabToSet.collider); // Remove the old collider, the type doesn't matter
AddComponent(prefabNew.collider.GetType()); // Add a new collider of this type.
While this is true, it might just lead the OP in the wrong direction, because while this does "add the same type of collider as is in prefacNew", it doesn't and can't set up the properties on it - I.e. copy the Component.
Components cannot be copied.
Yeah, Components can't be copied / cloned or change their parent GameObject. The only way is to manually copy the preferences of the component.
It's possible to do this in a generic way via reflection, but in case of Collider i would recommend to use a switch - case since there are only a few collider types available.
I can only think of 4 at the moment (BoxCollider, SphereCollider, CapsuleCollider, $$anonymous$$eshCollider)
Thanks, I got it working using this. Although it is unfortunate that you can't copy the components/preferences.
Answer by GuyTidhar · Jul 04, 2011 at 12:55 PM
// prefabsOldCollider and prefabsNewCollider are instances.
Collider prefabsOldCollider = prefabOld.collider;
Collider prefabsNewCollider = prefabNew.collider;
// When you call this, you what ever the collider is found on this game object
DestroyImmediate(prefabToSet.GetComponent<Collider>());
// When you call this, you create a new component of Collider type and assign it to prefabsNewCollider
prefabsNewCollider = prefabToSet.AddComponent<Collider>() as Collider;
You need a concrete Collider class like BoxCollider, SphereCollider or $$anonymous$$eshCollider. He wants to use the type of the prefabNew.collider but in this case the generic won't work. Generic parameters have to be constant.
@Bunny83 so, basically, we could fix Guy's fix simply by replacing the last line with (...) .AddComponent
, couldn't we? In other words: Is the DestroyImmediate
there also wrong? I can see it is different from yours.
Your answer
Follow this Question
Related Questions
how can i add another scripts functionality to a button on another script? 2 Answers
How to GetComponent from Class that uses Generics 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to get a component from an object and add it to another? (Copy components at runtime) 12 Answers