I’m trying to access the score variable in another class and I get this error.
Assets/JavaScript/script player.js(76,54): BCE0019: ‘score’ is not a member of ‘UnityEngine.Component’.
what is this error message mean?
this is my main class. In my other class which is call className the variable score is declared and assigned.
var obj = GetComponent(“className”);
function OnGUI()
{
GUI.Label(Rect(10,10,100,20),"Score: " + obj.score);
}
Hi KhmrDude,
I develop in C# and am not familiar with JavaScript, but GetComponent just returns a “Component” object. It looks like you need to type-cast obj to the proper type, with something like:
var obj = (className) GetComponent("className");
Edit: According to the docs, it looks like you can also use var obj = GetComponent(className); Same thing, but without the quotes.
no I have this message
ArgumentException: You are not allowed to call GetComponent when declaring a variable.
Move it to the line after without a variable declaration.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
script player…ctor () (at Assets/JavaScript/script player.js:71)
Give this a shot.
//global declaration
public var obj : className;
function Start()
{
obj=GetComponent("className") as className;
Debug.Log(obj.score);
}