Delete a script component if object has already one

Hello everyone!
For some reason i need to write a code to delete a script component from an istanced object if this object already has this script. So it’s impossible that the object has two instances of the same script. How can i code it? I tryed

if (gameObject.GetComponent("ObjectMovement") > 1){
    Destroy(gameObject.GetComponent("ObjectMovement"));
}

but obviously it doesn’t work since i can’t compare a component to an integer. How can i do? Thanks in advance!

Try to get the component, and if its not null, then delete the component that you recieved from the GetComponent function.
Something like this:

ScriptName other = gameObject.GetComponent<ScriptName>();
if(other != null)
{
Destroy(other);
}

If you want to look to see if you have more than one component of the same type, then use the GetComponents function, which returns an array with the components. Then you can see how many there is in the array, if any.

ScriptName []other = gameObject.GetComponents<ScriptName>();

if(other.length > 0)
{
 for(int i = 0; i < other.length; ++i)
 {
   Destroy(other*);*

}
}
The pseudo examples are for scripts, but the idea should work for other types of components as well.
Good luck!

Hey

if (gameObject.GetComponent("ObjectMovement"))

would implicit check if you can get the Component.
Explicit it’s a check versus ‘null’:

if (gameObject.GetComponent("ObjectMovement") != null )