x


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!

more ▼

asked Jul 04 '11 at 11:19 AM

KodaL gravatar image

KodaL
31 5 5 7

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.
more ▼

answered Jul 04 '11 at 01:30 PM

Bunny83 gravatar image

Bunny83
45k 11 48 206

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)
10|3000 characters needed characters left
// 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;
more ▼

answered Jul 04 '11 at 12:55 PM

GuyTidhar gravatar image

GuyTidhar
2.2k 4 8 13

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

@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.

Apr 26 at 04:42 PM Cawas
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4141
x405
x347
x77
x41

asked: Jul 04 '11 at 11:19 AM

Seen: 2376 times

Last Updated: Apr 26 at 04:42 PM