x


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.

more ▼

asked Dec 22 '11 at 11:42 PM

CraftableQue gravatar image

CraftableQue
16 4 5 5

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Dec 22 '11 at 11:53 PM

Aleron gravatar image

Aleron
166 5 7 8

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3325
x532
x91
x86
x7

asked: Dec 22 '11 at 11:42 PM

Seen: 1349 times

Last Updated: Dec 22 '11 at 11:53 PM