Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

AddComponent Confusion

Discussion in 'Scripting' started by CharlieVolpe, Apr 20, 2015.

  1. CharlieVolpe

    CharlieVolpe

    Joined:
    Apr 20, 2015
    Posts:
    4
    Hey Guys,

    I am having some major issues with this. I have made several attempts which I will lay out. I am really looking for a deeper understanding of this.

    My code originally looked like this:
    Code (CSharp):
    1. public EB( string str, GameObject dummy ) {
    2.   Action comp = (Action)dummy.AddComponent(str);
    3. }
    Unity's Auto-Update changes the code to this:
    Code (CSharp):
    1. public EB( string str, GameObject dummy) {
    2.   Action comp = (Action)UnityEngineInternal.APIUpdaterRuntimeServices.AddComponent(dummy, "Assets/EB.cs (18,27)", str);
    3. }
    However, this throws a warning:
    warning CS0618: `UnityEngineInternal.APIUpdaterRuntimeServices.AddComponent(UnityEngine.GameObject, string, string)' is obsolete: `AddComponent(string) has been deprecated. Use GameObject.AddComponent<T>() / GameObject.AddComponent(Type) instead.

    I then tried using System.Type.Gettype():
    Code (CSharp):
    1. public EB( string str, GameObject dummy {
    2.   Action comp = (Action)dummy.AddComponent( System.Type.GetType(str) );
    3. }
    This makes the warning go away but when the function is actually called it throws a warning:
    AddComponent asking for invalid type
    Anything that tries to reference 'comp' errors out.

    The other information I found was that I am supposed to be using something similar to this code:
    Code (CSharp):
    1. public EB( string str, GameObject dummy ){
    2.   Action comp = dummy.AddComponent<[insert_type_here]>();
    3. }
    However, I need the type to be based off my string..

    I seriously hope you can give me more insight on this so I can actually wrap my head around this issue.

    Cheers,
    Charlie
     
    Last edited: Apr 20, 2015
  2. Steve-Tack

    Steve-Tack

    Joined:
    Mar 12, 2013
    Posts:
    1,240
  3. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,302
    So, it needs the assembly name, because without it it can't tell what type you are looking for.

    I did a little googling and testing, and I'm still not entirely sure how to get the assembly names consistently for all types in a uniform way.. If someone wants to shed some light, that would be nice.


    Code (csharp):
    1.  
    2.         //Unity Type
    3.         System.Type AudSourceType = System.Reflection.Assembly.GetAssembly(typeof(GameObject)).GetType("UnityEngine.AudioSource");
    4.  
    5.         //Custom Class Type
    6.         System.Type CustClassType = System.Type.GetType ("YourScriptName,Assembly-CSharp");
    7.  
     
    stormwaver and CharlieVolpe like this.
  4. CharlieVolpe

    CharlieVolpe

    Joined:
    Apr 20, 2015
    Posts:
    4
  5. CharlieVolpe

    CharlieVolpe

    Joined:
    Apr 20, 2015
    Posts:
    4
    Hey NA-RA-KU,

    This is very interesting. My classes that I am trying to grab the type for are all based off custom classes so it looks like maybe I would be using the 'Assembly-CSharp' version there?

    Cheers,
    Charlie
     
  6. CharlieVolpe

    CharlieVolpe

    Joined:
    Apr 20, 2015
    Posts:
    4
    Hey NA-RA-KU,

    That seems to have worked pretty well. Since I knew that all of them were 'Assembly-CSharp's. Thanks! Let me know if you find a uniform version! :)

    Cheers,
    Charlie