- Home /
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.
Answer by Aleron · Dec 22, 2011 at 11:53 PM
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.
Your answer
Follow this Question
Related Questions
expressions in statements must only be executed for their side effects 1 Answer
C# Unity 3D Lock Rotation but allow Rotation of Parent Object 1 Answer
What is wrong with this script? 2 Answers
BeginScrollView question on auto increasing 1 Answer
Adding new script, copying values, removing old script without selecting GO. 1 Answer