Make a script disable itself?

I have a script that should only run at the start, and then after it’s done doing its thing, it should get disabled, since it has stuff in the Update method, and I don’t want it to keep running and clogging up the CPU.

Is there anything simpler than object and component references? Like a command that disables the script running the command?

1 Like

You might try this.enabled = false;

Not sure if that would work, never tried it.

4 Likes

That disables the whole object the script is attached to.

It would work if I attached it to an object, and removed that object. But I need to be able to re-enable the script, for stuff like respawning at the start.

Odd, just tested it and it disabled the single script.
Yeah, the single script got disabled and the other scripts on the same object kept running.

Are you saying the gameobject became inactive?

1 Like

nwm, I was trying to change something before, and I accidentally left a part of the code at targeting the object the script is attached to, whereas it was supposed to target something else.

Thanks for the help. :slight_smile:

Setting enabled = false; is correct. :slight_smile:

4 Likes

Completely would, and is correct.