Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by KodaL · Jul 04, 2011 at 11:19 AM · c#editor-scriptinggetcomponentaddcomponentcs0246

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Waz · Jul 04, 2011 at 02:25 PM 0
Share

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.

avatar image Bunny83 · Jul 04, 2011 at 03:35 PM 0
Share

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)

avatar image KodaL · Jul 05, 2011 at 12:36 AM 0
Share

Thanks, I got it working using this. Although it is unfortunate that you can't copy the components/preferences.

avatar image
0

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;
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jul 04, 2011 at 01:35 PM 1
Share

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.

avatar image cregox · Apr 26, 2013 at 04:42 PM 0
Share

@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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges