- Home /
How delete or remove a component of an GameObject?
I am trying to remove this component.
I've tried
Component.Destroy(script); this.gameObject.Component.Destroy(script); etc etc. I am using JavaScript
Also, while I am at this how do you add one? It said it can't find my script to add as a component.
Answer by Jennifer1 · Dec 20, 2014 at 05:36 PM
simply use Destroy() function.
// Kills the game object
Destroy (gameObject);
// Removes this script instance from the game object
Destroy (this);
// Removes the rigidbody from the game object
Destroy (rigidbody);
// Kills the game object in 5 seconds after loading the object
Destroy (gameObject, 5);
// When the user presses Ctrl, it will remove the script
// named FooScript from the game object
function Update () {
if (Input.GetButton ("Fire1") && GetComponent (FooScript))
Destroy (GetComponent (FooScript));
}
this is the best post ive ever read for how compact and clear all the examples are. thank you!
Answer by DreamGro · Jul 11, 2017 at 12:35 PM
If you wrote a script you did not have to say GetComponent twice. Because it's already defined! ;-)
Example:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ExampleClass : MonoBehaviour
{
public ExampleScript exp;
void Start ()
{
exp = GetComponent<ExampleScript>();
}
void Update ()
{
Destroy(exp);
}
}
Answer by himanshugupta159 · Jan 03, 2019 at 12:23 PM
Check the link below.The blog teaches the way to create a gameobject,add components,remove componenets and deleting gameobject on runtime.
https://unfragilecoding.blogspot.com/2018/12/creating-gamobject-adding-components.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613090811im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I disable Script on FP Controller? (Solved) 2 Answers
destroy hinge joint component from another object in javascript 2 Answers
Why am I getting this error ? 2 Answers
Activate a component when an animation clip in an animator is played? 0 Answers
Script.Enabled does not work in Unity 5+ 2 Answers