x


Enabling / Disabling C# Scripts - Why am I getting the error "not a member of 'UnityEngine.Component'"?

Hello,

I'm having a problem with disabling scripts with JavaScript.

Usually this works:

var code : SampleCode = GetComponent(SampleCode);
code.enabled = false;

This seems to only work on javascript. When I try this on c# scripts it says that SampleCode is not a valid type.

Any ideas? I've tried the GetComponent("SampleCode").enabled = false; method but it doesn't work either - it gives the error - BCE0019: 'enabled' is not a member of 'UnityEngine.Component'.

Thanks!
Christian Stewart

more ▼

asked Mar 14 '10 at 07:57 AM

Christian Stewart gravatar image

Christian Stewart
165 13 16 21

Thanks, but the firs comment was correct.you have to place c scripts in the standard assets folder before you can reference them.

Mar 16 '10 at 02:09 PM Christian Stewart
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

You have to get C# scripts to compile earlier so Javascript knows about them. See the docs: http://unity3d.com/support/documentation/ScriptReference/index.Script_compilation_28Advanced29.html

more ▼

answered Mar 14 '10 at 08:47 AM

Eric5h5 gravatar image

Eric5h5
81.5k 42 133 529

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

Eric5h5's comment is important about compilation order, if you're trying to disable a c# script from a Javascript script.

However it possibly sounds as though you're trying to re-write this in C#. If this is the case, the problem is happening because "GetComponent" returns a type of "Component" (which Javascript automatically casts to the correct type for you, but c# does not). In C#, you need to do either this:

SampleCode code = (SampleCode) GetComponent(typeof(SampleCode));

or use the newer generic version:

SampleCode code = GetComponent<SampleCode>();

Then you'll be able to use:

code.enabled = false;
more ▼

answered Mar 14 '10 at 09:34 AM

duck gravatar image

duck ♦♦
41.4k 95 152 415

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

x2030
x313
x222
x199
x112

asked: Mar 14 '10 at 07:57 AM

Seen: 5185 times

Last Updated: Mar 14 '10 at 07:35 PM