- Home /
Can't destroy script attached to GameObject, incomprehensible type error
In my scene, I have an instance of FPSController from the Characters standard assets named player
. The following behavior happens when attempting to remove a script from it:
Component FpsControllerScript = player.GetComponent(typeof(MonoBehaviour));
// prints "UnityStandardAssets.Characters.FirstPerson.FirstPersonController"
Debug.Log(FpsControllerScript.GetType().ToString());
// prints exactly the same thing
Debug.Log(typeof(FirstPersonController).ToString());
// prints "false", wtf???
Debug.Log(FpsControllerScript.GetType() == typeof(FirstPersonController));
// Does nothing
Destroy(player.GetComponent(typeof(FirstPersonController)));
// Destroys script
Destroy(player.GetComponent(FpsControllerScript.GetType()));
Any ideas? This type error doesn't make any sense.
Answer by Gaffail · Aug 08, 2016 at 04:00 PM
player.getComponent().enabled = false; will disable the script. Destroy deletes a gameobject and not a script.,
The OP asked to destroy the component, not disable it, and for that you use Destroy().
Hmm, never used it for that, but I think this might be a problem with the GetComponent part. If he tries Destroy(GetComponent(typeof(player.FpsControllerScript))); ins$$anonymous$$d?
Answer by GilbertoBitt · Aug 08, 2016 at 04:00 PM
first of all u din't put the errors u r getting.. and second i think the problem must be because fpsController is a required component of another script!
see: https://docs.unity3d.com/ScriptReference/RequireComponent.html
Answer by tanoshimi · Aug 08, 2016 at 04:13 PM
I'm not sure which bit you're confused by, but judging by your WTF comment, it's why
FpsControllerScript.GetType() != typeof(FirstPersonController);
The reason is quite simple: typeof(FirstPersonController) is a FirstPersonController
, whereas FpsControllerScript is a generic Component
(you declare it as such on the very first line of your script).