- Home /
How can I enable and disable scripts at runtime?
Im trying to disable two different scripts during runtime, in this case I need to diable the mouse Look and playerMovementV1 script on the player object. How would I do this?
this question has been answered and asked many times. first you get the refernce of the component and set its enbaled to false;
(gameObject.GetComponent("$$anonymous$$ouseLook") as $$anonymous$$ouseLook).enabled = false;
http://answers.unity3d.com/questions/26844/enabledisable-specific-components.html http://answers.unity3d.com/questions/649162/how-to-enable-and-disable-script.html http://answers.unity3d.com/questions/28362/find-gameobject-and-activatedeactivate-it.html http://answers.unity3d.com/questions/672694/unable-to-enable-script.html http://answers.unity3d.com/questions/433018/enable-disable-object-script-error.html http://answers.unity3d.com/questions/529866/disable-gameobject-at-other-screen-with-code-.html
didnt even use google.
Answer by Punkjim420 · Mar 26, 2014 at 11:57 AM
ahhh thats unityscript. for c# youd do something like this:
public Player playerScript;
and then
playerScript.enabled = false;
in your code somewhere like an if statement maybe.
@Pumkjim420 yeah you need to get the reference of the code using GetComponent first. otherwise you would just get a null reference error because player Script isn't equal to anything, its null until you assign it.
ah ok, then i stand corrected. I usually use bools to disable lines of scripts anyways. ;)
Answer by hdr25 · Jan 12, 2017 at 09:21 AM
Things are different now in Unity 5 Its simple .. First you have to get that component and then get access to its script..
GameObject.FindGameObjectWithTag ("YOUR GAME OBJECT").GetComponent ().enabled = false;
That's exactly the same as it has been in previous versions of Unity, and as was already answered nearly 3 years ago...
Your answer
Follow this Question
Related Questions
Enable/Disable Game Object With GUI Button 1 Answer
Enabling loop and disabling loop 1 Answer
[solved]how can i enable or disable a component of instantiated objects? 5 Answers
error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer 3 Answers
image not enabling 0 Answers