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 /
This question was closed Dec 10, 2015 at 12:37 AM by GimLee.
avatar image
1
Question by GimLee · Dec 07, 2015 at 07:58 PM · unity 5playerprefscomponent

AddComponent changed after upgrading Unity

I've just upgraded from Unity 4 to 5.

It has changed a few lines of code. But the newly generated code is givin warnings.

Let me first show the original code, which worked perfectly fine.

 void Start () {
 
         a1 = gameObject.AddComponent(PlayerPrefs.GetString ("animal1") + "Functions") as IAnimalFunctions;
 
         currentAnimal = a1;
 
         Animal1Shape();
     }

I tried to change the code back to this, but it gave me the following error:

Assets/Scripts/Animals/AnimalContainer.cs(21,25): error CS0619: UnityEngine.GameObject.AddComponent(string)' is obsolete: GameObject.AddComponent with string argument has been deprecated. Use GameObject.AddComponent() instead. (UnityUpgradable).'

After upgrading, the above was changed into the following.

 a1 = UnityEngineInternal.APIUpdaterRuntimeServices.AddComponent(gameObject, "Assets/Scripts/Animals/AnimalContainer.cs (16,8)", PlayerPrefs.GetString ("animal1") + "Functions") as IAnimalFunctions;

And this gives me a warning:

[Assets/Scripts/Animals/AnimalContainer.cs (16,8)] Component type 'SheepFunctions' found on caller assembly. Consider replacing the call method call with: AddComponent()UnityEngine.Debug:LogWarningFormat(String, Object[]) UnityEngineInternal.APIUpdaterRuntimeServices:ResolveType(String, Assembly, String) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineInternal/APIUpdaterRuntimeServices.cs:45) UnityEngineInternal.APIUpdaterRuntimeServices:AddComponent(GameObject, String, String) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineInternal/APIUpdaterRuntimeServices.cs:27) AnimalContainer:Start() (at Assets/Scripts/Animals/AnimalContainer.cs:16)


The PlayerPrefs.GetString ("animal1") + "Functions" is equal to "SheepFunctions" which is another class.

How do I fix this problem? :)

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 static_cast · Dec 07, 2015 at 08:03 PM 0
Share

By judging the source you've given us, it seems like the way you've structured your classes may not be the best way. Have you tried adding the Functions component and then changing it based on the animal?

avatar image static_cast · Dec 07, 2015 at 08:05 PM 0
Share

Also, here's a similar problem: http://answers.unity3d.com/questions/254479/getcomponent-from-string-name.html

Hope this helps

avatar image GimLee · Dec 07, 2015 at 09:06 PM 0
Share

It was working pretty fine before I updated. I'd like to do it this way still. Why can't I just define a type by a string value anymore?

1 Reply

  • Sort: 
avatar image
1

Answer by steakpinball · Dec 07, 2015 at 09:23 PM

This may help. http://answers.unity3d.com/answers/923278/view.html

Safe way:

 if ("SheepFunctions" == PlayerPrefs.GetString ("animal1") + "Functions") {
   gameObject.AddComponent<SheepFunctions>();
 }

Unsafe way:

 gameObject.AddComponent(System.Type.GetType(PlayerPrefs.GetString ("animal1") + "Functions"));
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 Bunny83 · Dec 08, 2015 at 12:31 AM 0
Share

Your "unsafe way" is not more unsafe than the old AddComponent string version. You might want to do:

 var type = System.Type.GetType(PlayerPrefs.GetString ("animal1") + "Functions");
 if (type != null && typeof(Component).IsAssignableFrom(type))
     gameObject.AddComponent(type);

This is the safest way and the equivalent of the old string version. You can use an extension method like this:

 public static class GameObjectExtension
 {
     public static Component AddComponentByString(this GameObject aGameObject, string aTypeName)
     {
         var type = System.Type.GetType(aTypeName);
         if (type == null || !typeof(Component).IsAssignableFrom(type))
             return null;
         return aGameObject.AddComponent(type);
     }
 }

With that you can simply use "AddComponentByString" just like the old string version:

 gameObject.AddComponentByString(PlayerPrefs.GetString ("animal1") + "Functions");

Since the old AddComponent method that takes a string does still exist (at least in Unity version 5.2.1) I used a different name for the extension method.

avatar image GimLee Bunny83 · Dec 10, 2015 at 12:17 AM 0
Share

I can't really get this to work.

gameObject.AddComponent(type) as IAnimalFunctions;

Gives me the following error:

error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement

avatar image GimLee Bunny83 · Dec 10, 2015 at 12:36 AM 0
Share

Never$$anonymous$$d I fixed it, thanks. Great help.

Follow this Question

Answers Answers and Comments

42 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

Related Questions

How can I collect playerprefs values? 0 Answers

2D Animation does not start 1 Answer

enable and disable script on Unity gameobject 0 Answers

Access water4 scripts via my script? 1 Answer

Is there a way to dynamically attach a script to a GameObject during runtime? 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