Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by gselfish · Mar 31, 2015 at 10:49 AM · unity 5scripting problemaddcomponent

Attaching a script at runtime

Hi all,

I would need to add a script to a gameobject based on its name, which is stored in a string. So, for example, if an object called "zombie" enters the field, I have to attach zombie.cs to it.

I was planning on using something like:

 gameObject.AddComponent(gameObject.name);

but I've just realized that this method has been deprecated in Unity 5. Now, the script class can only be specified as a generic class.

One possible workaround would be to create a method that explicitly calls AddComponent with the right type for each possible string, but I was wondering it there's a more elegant solution.

So far I've tried:

         Type script = Type.GetType(gameObject.name);

         gameObject.AddComponent<script>();

and:

 gameObject.AddComponent<Type.GetType(gameObject.name)>();

but they both give different compilation errors.

Any idea?

Comment
Add comment · Show 2
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 Nerevar · Mar 31, 2015 at 11:21 AM 0
Share

from what I know in unity 4.6 it was:

c#:

 gameObject.AddComponent<YourScriptNameOrClassName>();

JS:

 gameObject.AddComponent("YourScriptNameOrClassName");

it seems you are mixing the methods.

the script you want to attach derives from monobehavior?

Do you really need to use the string reference to attach your script?

I am not using unity5 yet sorry :p

avatar image gselfish · Mar 31, 2015 at 06:40 PM 0
Share

The gameObject.AddComponent(string) syntax doesn't work any more in Unity 5: http://blogs.unity3d.com/2015/01/21/addcomponentstring-api-removal-in-unity-5-0/ (I wish I had found this before...)

2 Replies

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

Answer by NoseKills · Mar 31, 2015 at 04:12 PM

In Unity 5 There's a non generic version of AddComponent(Type tp) too

 gameObject.AddComponent(Type.GetType(gameObject.name));
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 gselfish · Mar 31, 2015 at 06:39 PM 1
Share

Thank you, that's exactly what I needed! The following code, with a check added as a safety measure, is working as expected:

 var scriptAsset = AssetDatabase.FindAssets (cardName);
 if (scriptAsset.Length > 0)
 {
     gameObject.AddComponent(Type.GetType(cardName));
 }
 else
 {
     Debug.Log (cardName + " not supported!");
 }
avatar image NoseKills · Mar 31, 2015 at 10:10 PM 0
Share

If the answer is correct, please click the tickmark next to it to mark this question answered

avatar image
0

Answer by astracat111 · Oct 14, 2016 at 05:54 AM

It's "MonoScript" not "script" now, I don't know why.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

(C#) how do i make a first-person controller script that just moves me on the X & Y axis? 1 Answer

"StateMachineBehaviour" Not working for sub sates ? 0 Answers

Polymorphism for decisions in a dialogue editor 1 Answer

Program not run/ refresh in unity,Not Run Program 1 Answer

Unity Error Script trying to use Chartboost 0 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