- Home /
beginner syntax error..
"BCE0019: 'xxxx' is not a member of 'UnityEngine.Component'."
I am currently trying to follow some basic tutorials for a better understanding of scripting within unity in javascript but recently I have come across an error witch I have been unable to solve. In this case I am trying to access a variable from one script to use in a second one by using the GetComponent function but it always gives me an error anytime I use the GetComponent function be it a variable or even if i try to access other functions(I get the same error).
Example
// player script
var lives : int = 3;
var playerSpeedHorizontal : float = 10.0;
var playerSpeedVertical : float = 10.0;
var horMin : float = -4.0;
var horMax : float = 4.5;
var verMin : float = -3;
var verMax : float = 2.7;
var projectile : Transform;
var sockets : Transform;
function Update ()
{
var transV : float = Input.GetAxis("Vertical")*playerSpeedVerticalTime.deltaTime;
var transH : float = Input.GetAxis("Horizontal")playerSpeedHorizontal*Time.deltaTime;
transform.Translate(transH,transV,0);
transform.position.x = Mathf.Clamp(transform.position.x,horMin,horMax);
transform.position.y = Mathf.Clamp(transform.position.y,verMin,verMax);
if(Input.GetKeyDown("space"))
{
Instantiate(projectile,sockets.position,sockets.rotation);
}
}
//script asteroid
var asteroidSpeed : float = 6.0;
var explosion : Transform;
function Update ()
{
transform.Translate(Vector3.downasteroidSpeedTime.deltaTime);
if(transform.position.y <= -5)
{
transform.position.y = 5;
transform.position.x = Random.Range(-4.0,4.5);
}
}
function OnTriggerEnter(other : Collider)
{
if(other.gameObject.tag == "player")
{
other.GetComponent("script player").lives-=1;
}
ResetEnemy();
}
function ResetEnemy ()
{
transform.position.y = 5;
transform.position.x = Random.Range(-4.0,4.5);
}
In this example I am trying to substract the variable lives found in script one accessing it with script two by using the GetComponent function.But when it compiles it gives me the BCE0019 error.This error also appears even if i create a separate function to substract the variable lives.
Any help and an explanation would be appreciated.
@$$anonymous$$ander: It is public. In UnityScript the default visibility is public. In C# it's private.
Answer by Hybris · Sep 24, 2012 at 04:35 PM
other.GetComponent("script player").lives-=1;
that should be without the "" so like this:
other.GetComponent(script player).lives-=1;
but that might make some errors and that is because the name of the script has a space in between, try to avoid spaces, what I like to do is use underscores so the name would be: script_player or what you could do is use capitals like ScriptPlayer, personally I would've named it PlayerScript so:
other.GetComponent(script_player).lives-=1;
Oh, and please format your code, it was unreadable like that, formatting is like using tabs and stuff.
-Hybris
exactly, script names (which are class names) can't include spaces. Also when using the string version of GetComponent, the compiler doesn't know the actual type of the component. When using the "type" version, the compiler knows the component that is returned be GetComponent is of this specific type.
Script names can technically include spaces, and they will work, however you will not be able to write GetComponent($$anonymous$$y Script). Ins$$anonymous$$d you'd have to use quotes: GetComponent("$$anonymous$$y Script"). So I'd recommend not ever using spaces in script names.
They really work? What class name is generated for such scripts?
Another thing i can put on my "list of things i have to test someday" :D
They work in Unityscript, anyway. The class name is the same as the script name, so it will have a space in it. Not that you'd want to, of course....
Answer by CrayOn · Jul 27, 2013 at 05:12 PM
It's a casting problem.
(other.GetComponent(scriptPlayer) as scriptPlayer).lives -=1;
The question is using Unityscript, so the correct way is
other.GetComponent(scriptPlayer).lives -= 1;
You don't need to use "as X" in Unityscript, and the generic version is unnecessary (and slightly slower).
Hmm i don't know i couldn't get things done until i did it like in the comment.
Why i can not post comment like this (without ') it is not allowing? other'.'GetComponent'.'<'scriptPlayer'>'()'.lives -=1;
The text parser on this site has issues, and the tags cause stuff to get eaten unless you use spaces. other.GetComponent< scriptPlayer >().lives -= 1;
. If you're using C#, feel free to do that or use the "as scriptPlayer" casting, but as I mentioned there's no reason to do any of that in Unityscript.
Thank you, i will use spaces... I am using JavaScript for coding.
Your answer
Follow this Question
Related Questions
Some problems after switch the game for iOS mode 2 Answers
error for array functions 2 Answers
finish line script 3 Answers
transform.position not a member??? 2 Answers