- Home /
is not a member of 'UnityEngine.Component'
When using #pragma strict The following code causes the error: trap_magnet1.js(101,46): BCE0019: 'target' is not a member of 'UnityEngine.Component'.
laser1.GetComponent("LightningBolt").target = playerBallCollider.transform;
My script is a javascript. Lightningbolt is a cs script. target in that script is a transform.
Answer by Kryptos · Jan 19, 2012 at 08:09 PM
GetComponent() returns an object (of type Component), you need to perform a cast to be able to access your script variables:
(laser1.GetComponent("LightningBolt") as LightningBolt).target = playerBallCollider.transform;
I'm not a JS user (I prefer C#), so maybe this one-line code is not allowed and you will have to split it in two:
var bolt : LightningBolt = laser1.GetComponent("LightningBolt") as LightningBolt;
bolt.target = playerBallCollider.transform;
This does not work. But I am expiriencing issues with references to cs scripts/classes.
For example: var bolt : LightningBolt; produces the error: does not denote a valid type ('not found'). I have the same issue with other js scripts that need to acces a cs script. The lightningbolt scipt and class uses the same name: public class LightningBolt : $$anonymous$$onoBehaviour
$$anonymous$$ake sure in $$anonymous$$onoDevelop, that project Assembly-UnityScript references the project Assembly-CSharp. If not you should edit the references.
Anyway, this is bad practice to make JS and CS script in the same project. I recommend to translate every scripts to the language of your choice.
Thanks, your code works. Looks like its best to place cs scripts in the plugins folder (or the standard assets folder).
Your answer
Follow this Question
Related Questions
getting a variable from a c# script with a Js 2 Answers
passing variable from javascript to C# 1 Answer
trying to set a javascript variable from c sharp 1 Answer
Accessing JavaScript variable from C# and vice versa. 3 Answers
C# GetComponent 3 Answers