- Home /
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.
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?
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.
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
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.
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.
Answer by Paulius-Liekis · Sep 15, 2014 at 08:24 AM
Destroy(GetComponent<MyScript>());
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.
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
"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).
Ah, sorry. Didn't see that. Why do you get that many scripts on the object?
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!
@EClancy Isn't it easier to have different prefabs for each character?
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;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613163015im_/https://answers.unity.com/themes/thub/images/avi.jpg)