Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by sisse008 · Jan 11, 2021 at 04:40 PM · type

Add component of unknown type

The following code does not work:

 GameObject g = new GameObject("Player");
 System.Type t = typeof(AutomatedPlayer);
  if (player_isAutomated)
         p = g.AddComponent(t);


AutomatedPlayer is a class I defined which inherits from MonoBehaviour. Why is this not working?

Thank you

Comment
Add comment · Show 3
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 Hellium · Jan 11, 2021 at 05:13 PM 1
Share

Please, define "this is not working". Do you have errors? Isn't the component attached? Are you sure you enter in the if block?

In your precise case, why not using

 GameObject g = new GameObject("Player");
 if (player_isAutomated)
      p = g.AddComponent<AutomatedPlayer>();
avatar image sisse008 Hellium · Jan 11, 2021 at 10:29 PM 0
Share

i dont want to do:

  p = g.AddComponent<AutomatedPlayer>();

i want to do:

  p = g.AddComponent(t);

where t is a variable of type System.Type (or whatever it needs to be for this to work).

That way, i can have a list of types, say AutomatedPlayer and HumanPlayer and I can set t to an element from that list.

Thank you

avatar image Hellium · Jan 11, 2021 at 10:56 PM 1
Share

I just did a test with those simples classes

 public class A: $$anonymous$$onoBehaviour
 {
     private void Start()
     {
         System.Type type = typeof(B);
         gameObject.AddComponent(type);
     }
 }
 
 public class B : $$anonymous$$onoBehaviour
 {
 }

and it works, so you are most likely forgetting to indicate some crucial information.

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by CmdrZin · Jan 12, 2021 at 02:28 AM

try

 g.AddComponent<ScriptName>(AutomatedPlayer);    
Comment
Add comment · 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
0

Answer by Bunny83 · Jan 12, 2021 at 03:55 AM

Like Hellium said in the comments above there's no reason why AddComponent(typeof(YourClassName)) shouldn't work when AddComponent<YourClassName>() works. That's because the generic versions of AddComponent / GetComponent have been added just for convenience. Under the hood the generic version does just use the System.Type version anyways. And that's not just in a metaphorical sense but literally.


So if it doesn't work for you there has been other issues. Either your type can not be attached at all because it may not be compiled due to compiler errors or you haven't saved the file so the content the compiler sees is not up to date. Another reason why it can't be attached could be that you haven't placed each of your MonoBehaviour derived types you wish to add to a gameobject in its own C# file where the file name matches the class name. This requirement still holds true.


The other option why "it doesn't work", as Hellium said, could be that your code doesn't actually run because your if condition may not be true.


That's why it's important to be more specific what exactly doesn't work and how you know that. So what have you done already to debug your issue? Another thing that isn't clear is what's the type of your variable "p"?

Keep in mind that AddComponent with a System.Type parameter has "Component" as return type. So without an explicit cast "p" could only be of type Component or UnityEngine.Object. Of course you could not access any class specific members with that variable. In order to access anything from a class you have to know its type or you have to know what interface(s) the class implements.

Comment
Add comment · 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

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

112 People are following this question.

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

Related Questions

Boo help: method not defined 4 Answers

Class, struct, or interface method must have a return type 1 Answer

C# conversion of JavaScript, problems with nested class as a custom type 1 Answer

Scripting Generic stat modifiers. 0 Answers

Is there a way for subclasses to inherit variable type and name but have their own values? 1 Answer


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