alexo1001
alexo1001

Reputation: 307

Disabling a Script attached to a game object in Unity C#

I have an update function that will check if something is equal to true and if it is equal to true it will run some code but then I want the whole script to be disabled. Please help, thanks!

Upvotes: 7

Views: 47202

Answers (3)

Codemaker2015
Codemaker2015

Reputation: 1

You can disable script component by using the following syntax,

GameObject.Find("Cube").GetComponent<MoveObject>().enabled = false;

Here I disable the MoveObject script from the object named Cube.

Upvotes: 0

Kamran Mohazzab
Kamran Mohazzab

Reputation: 11

This will work:

public GameObject otherobj;//your other object
public string scr;// your secound script name
void Start () {
    (otherobj. GetComponent(scr) as MonoBehaviour).enabled = false;
}

Upvotes: 0

Valentin
Valentin

Reputation: 5488

You should use an enabled field. Try following

gameObject.GetComponent<EmailSender >().enabled = false;

Upvotes: 13

Related Questions