|
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:
Code:
Please help, Thanks!
(comments are locked)
|
|
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(); Can be simplified: 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.
Jul 04 '11 at 02:25 PM
Waz
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, MeshCollider)
Jul 04 '11 at 03:35 PM
Bunny83
Thanks, I got it working using this. Although it is unfortunate that you can't copy the components/preferences.
Jul 05 '11 at 12:36 AM
KodaL
(comments are locked)
|
You need a concrete Collider class like BoxCollider, SphereCollider or MeshCollider. 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.
Jul 04 '11 at 01:35 PM
Bunny83
(comments are locked)
|
