x


Turn off a script?

Hello. I know you can enable/disable a game object through something like this, how can I do that with a script (in another object)? Thanks

var CS : GameObject;

function Update (){ if (Genre==1) {CS.SetActiveRecursively(true);}; }

more ▼

asked Dec 27, 2010 at 06:06 AM

Tyler 2 gravatar image

Tyler 2
1.1k 637 467 475

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

2 answers: sort voted first

you should use CS.GetComponent("ScriptName").enabled = false;

more ▼

answered Dec 27, 2010 at 08:13 AM

Kourosh gravatar image

Kourosh
1.6k 138 103 117

Thank you!! This was very helpful!!!

Jan 14, 2011 at 03:50 AM Heather

is that java or C#?

Jun 04, 2013 at 12:07 PM yu-gi-master

But in c# it says that GetComponent("ScriptName" doesn't have method " enabled" why so ??

May 16 at 09:15 PM yami7

Most components, including any scripts, have an "enabled" variable (gameobjects use SetActive() to turn them on/off in a similar way).

To access another script on the same gameobject:

 GetComponent<ScriptName>();

You should really store that in a variable in advance (like in the Start method) since you don't want to call GetComponent on the fly if possible:

 ScriptName otherScript;
 
 void Start()
 {
    otherScript = GetComponent<ScriptName>();
 }

Then assign the script's "enabled" variable to false:

 otherScript.enabled = false;

Or, like the above answer did it all together on one line without storing it in a variable:

 GetComponent<ScriptName>().enabled = false;

If the script was on another object, you'd need to use some method in front of GetComponent to access that specific gameobject. Like a variable that you've already assigned that gameobject to, or a function like GameObject.Find() or GameObject.FindWithTag(). Example:

GameObject.Find("Player").GetComponent().enabled = false;

May 16 at 09:37 PM theredace
(comments are locked)
10|3000 characters needed characters left

CS.GetComponent("ScriptName").enabled = false;

Errors:

BCE0019: 'enabled' is not a member of 'UnityEngine.Component'.

more ▼

answered Apr 16, 2013 at 10:28 AM

przem997 gravatar image

przem997
0

(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:

x4911
x535

asked: Dec 27, 2010 at 06:06 AM

Seen: 6419 times

Last Updated: May 16 at 09:38 PM