Remove and Add a Script via a Code

How do you add scripts via code? I have been having trouble with this and I can’t find a way to reference a script. Can you then use AddComponent()? And can you delete that code with Destroy()? I am trying to make it so when I pause my game it makes a script stop. The problem is that it is a sample script in C# and I don’t know how to disable it since I only am familiar with Javascript in Unity. Pausing won’t work alone. Thank you in advance.

If the logic you are trying to stop from running is in the Update() of the script, you can get the component and set its enabled member to false when you pause the game and then set it to true when you stop pausing the game. That will prevent the Update() from being run while the game is paused.

Otherwise, yes, you can use AddComponent() to add a component to a game object and GetComponent() combined with Destroy() to remove a component, but in most cases I’m guessing just setting the enabled member on the component will do what you want.