Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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
2
Question by N.A.K_FYP · Feb 16, 2014 at 04:13 PM · c#gameobjectcomponentvuforia

Runtime Add/Remove C# Script

Hello everyone,

I have stored the DestoryOnTouch.cs under "Scripts/ButtonPop_Controll/", the DestoryOnTouch.cs used to destory the object on touch and it is work if i add the script in unity editor, not runtime.

I have added the code shown as below in a parent node object named Workstation, there are number of child object, so i using a for loop to add the DestoryOnTouch.cs in runtime

However,it is not work, anyone can help me find the problem?

Thank you.

 if(mDele) 
 { int childs = transform.childCount;
  for (int i = childs - 1; i >= 0; i--)
  { transform.GetChild(i).gameObject.AddComponent("Scripts/ButtonPop_Control/DestoryOnTouch"); 
 } 
 }
 
 
 
  if(!mDele) 
 { int childs = transform.childCount;
  for (int i = childs - 1; i >= 0; i--) 
 { GameObject.Destroy(transform.GetChild(i).gameObject.GetComponent("Scripts/ButtonPop_Controll/DestoryOnTouch"));
  }
  }
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
15
Best Answer

Answer by whydoidoit · Feb 16, 2014 at 04:19 PM

AddComponent and GetComponent use the name of the class in the script, not a path to the script.

  AddComponent("DestroyOnTouch");

Or preferably:

  AddComponent<DestroyOnTouch>();

As this will give you a compile time error if the class cannot be found, rather than a runtime error.

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 Tanoshimi2000 · Jun 27, 2018 at 12:00 PM

@whydoidoit Not intending to hijack this thread, but since it's here, I don't want to start a new one and get scolded.

2018 took away the ability to use a string for the AddComponent function. How would I be able to load a script at runtime, if I don't know what the name of the script is?

I have an XML with a list of powers. When the player selects on, I need the script for that power added in so that when they press the button to activate their power, that script is called.

Any suggestions?

Comment
Add comment · Show 4 · 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 PizzaPie · Jun 27, 2018 at 12:32 PM 0
Share

Well if you read the docs on AddComponent you ll notice it takes as a parameter a Type var so, assu$$anonymous$$g you have the name(string) of said Type you should do something like this

 System.Type mType = System.Type.GetType("mTypeNameString");
 AddComponent(mType);
avatar image Tanoshimi2000 PizzaPie · Jun 27, 2018 at 08:48 PM 0
Share

Well that works. I had read the documentation, but I've never used GetType before, so that's the part I didn't get. The problem now, is that I cannot call any of the functions or methods of that script. AddComponent returns a Component, and so I cannot use: GetComponent(mType).Fire();

avatar image Sephnroth Tanoshimi2000 · Jan 17, 2019 at 02:31 PM 0
Share

Hi Tanoshimi - I've just stumbled across this thread looking for info on something else and I expect you've long since solved your issue but for anyone else you need to cast your component to the right type, so you could say (($$anonymous$$ySpellScript)GetComponent(mType)).Fire();

Show more comments

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

23 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

Related Questions

Multiple Cars not working 1 Answer

What happens when i Copy an object (From Programming point of view) 1 Answer

C# Adding Components From Other Gameobjects 3 Answers

GUI Button On Click Event 0 Answers

Disabling A Script on a GameObject From a Different Script 2 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