x


Component is null?

I have two scripts on an object. One references the other:

var otherScript : OtherCS = GetComponent(OtherCS);

The other script is C# monobehavior, this one is JS. The c# script is in the Plugins folder. The component IS there on the gameobject. Even so, I put a 'require' of that component in the script just in case.

For some reason, I'm getting back null for this. I also tried GetComponent("OtherCS") and gameObject.GetComponent(.....) but nothing seems to work. Very strange. I restarted, rebooted, same anomoly.

I'm getting no errors on script compile, and I've reimported the scripts without problem too. I have the 'other' script on another game object where it works just fine.

Anyone got any ideas what might cause this?

more ▼

asked Aug 27 '11 at 12:45 AM

DaveA gravatar image

DaveA
26.5k 151 171 256

The answer is not in what you have posted. I just tested exactly that and it works fine. The only way I could get it to fail like you did was for OtherCS to inherit from MonoBehaviour, add the script, then remove that inheritance. I was surprised Unity gave no error.

Aug 27 '11 at 01:51 AM Waz

Nothing to do with compilation order? Try to place the JS script in a new folder in Assets - it will compile last, and maybe it can "see" the CS script. I know, you should have compiler errors if OtherCS was unknown when the JS script was compiled, but it wouldn't be the first time Unity "swallowed" errors during compile time to spit them at runtime...

Aug 27 '11 at 02:05 AM aldonaletto

@aldonaletto: he's already ensured the order by placing OtherCS in the Plugins folder.

Aug 27 '11 at 02:48 AM Waz
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

This didn't answer my question but did solve my problem: I disinherited it from MonoBehavior to make it a normal class and just new OtherCS() it.

more ▼

answered Sep 08 '11 at 08:12 PM

DaveA gravatar image

DaveA
26.5k 151 171 256

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

x4167
x3461
x406

asked: Aug 27 '11 at 12:45 AM

Seen: 903 times

Last Updated: Sep 08 '11 at 08:12 PM