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 EClancy · Sep 15, 2014 at 01:34 AM · gameobjectobjectdisableremove

How do you remove a script from an object?

I have objects that should be able to add and remove scripts to themselves. I can add the scripts appropriately, but I currently can only disable the wants I want not to work. Is there a way to remove the script entirely?

I worry about this because even if I restart the game, the same scripts keep accumulating, meaning I quickly get 50 disabled scripts attached to one object.

I cannot simply use "destroy()" on the script, as Unity says it is preventing data loss.

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 rutter · Sep 15, 2014 at 01:34 AM 0
Share

Are these components that you add while the game is running? If so, you can just call Destroy() on them, and they'll be removed.

Some of the problems you describe make me think you're attaching those components while Unity is in editor mode. Is there a reason for that?

avatar image EClancy · Sep 15, 2014 at 06:53 PM 0
Share

I am attaching those objects at runtime, for example if I select the "fire" character, that will give me certain attributes. I then can select a different character, in which I need to remove that script and attach a different one.

Once again, I cannot simply use "destroy()" on the script, as Unity says it is preventing data loss.

4 Replies

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

Answer by Kiwasi · Sep 15, 2014 at 07:57 PM

Your problem is you are attaching and removing scripts from a prefab, not an instance in the scene. Unity is very reluctant to allow direct editing of prefabs with the Destroy command.

Two options

  1. Create an instance of the prefab in the scene. Disable it. Add and destroy the required components. Then use this instance as a pseudo prefab to instantiate from. Once you exit playmode the pseudo prefab will reset.

  2. Use DestroyImmediate. This will let you touch prefabs. The downside is it will let you touch prefabs. The wrong call can delete prefabs forever. Use with caution. The prefab will never reset itself, and you are stuck with the changes you make.

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
4

Answer by Paulius-Liekis · Sep 15, 2014 at 08:24 AM

 Destroy(GetComponent<MyScript>());
Comment
Add comment · Show 1 · 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 EClancy · Sep 15, 2014 at 06:53 PM 0
Share

Not sure if you only read the title, but once again:

I cannot simply use "destroy()" on the script, as Unity says it is preventing data loss.

avatar image
0

Answer by TheDDestroyer12 · Sep 15, 2014 at 08:48 AM

Why do you want to remove a script during runtime? If you absolutely have to do it, you might want to use this:

 go.getComponent("component name").setActive(false);


/TheDDestroyer12

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 EClancy · Sep 15, 2014 at 07:00 PM 0
Share

"I worry about this because even if I restart the game, the same scripts keep accumulating, meaning I quickly get 50 disabled scripts attached to one object."

This is why I want to remove the script. Even between plays, they are accumulating.

While this does not solve the problem I'm worried about, it solves other problems with variables that the "enabled" field doesn't (because there is no update function anyway, and that's all enabled does).

avatar image TheDDestroyer12 · Sep 15, 2014 at 07:15 PM 0
Share

Ah, sorry. Didn't see that. Why do you get that many scripts on the object?

avatar image EClancy · Sep 18, 2014 at 05:14 PM 0
Share

Because every time the player picks a new character, it assigns a new script to it. I hoped for an easy way to also remove the scripts, so check out the accepted answer to see how!

avatar image TheDDestroyer12 · Sep 18, 2014 at 05:24 PM 0
Share

@EClancy Isn't it easier to have different prefabs for each character?

avatar image
0

Answer by VesuvianPrime · Sep 15, 2014 at 07:39 PM

Perhaps you would benefit from the following methods:

 public static T AddMissingComponent<T>(this GameObject extends)
     where T : Component
 {
     T existing = extends.GetComponent<T>();
     if (existing == null)
         existing = extends.AddComponent<T>();
     return existing;
 }

and

 public static Object SafeDestroy(Object target)
 {
     if (Application.isEditor)
         Object.DestroyImmediate(target);
     else
         Object.Destroy(target);

     return null;
 }
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

7 People are following this question.

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

Related Questions

Help With Disabling/Enabling single GameObject 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to remove objects from a list ? 3 Answers

Removing objects from an array 2 Answers

Drag Rigid Body No Distance Limit and how to add Mouselock to a scrip 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