- Home /
BCE0019: 'GetComponent' is not a member of 'Object'.
I get this error when I tried to add score on death. how can I fix it? I'll type in the enemy health script and the score script. Here we go.
pragma strict
var thisObject : Transform; var Health = 100; var WillDrop : boolean = false; var ObjectToDrop : GameObject[]; //[] means that you can use more than one objects var otherObj = new Array(); var isDead : boolean = false;
function Start()
{
WillDrop = Random.value < 0.10; //Dropchance of 10%
}
{
otherObj[0] = GameObject.FindGameObjectWithTag ("Score");
}
function ApplyDammage (TheDammage : int)
{
Health -= TheDammage;
if(Health <= 0)
{
isDead = true;
}
}
function Dead()
{
if(WillDrop == true) {
var obj : GameObject = ObjectToDrop[Random.Range(0, ObjectToDrop.length)]; //Randomize a prefab to drop between Medkit, ammobox and AllInOne kit
var pos: Transform = thisObject;
Instantiate(obj, pos.position, pos.rotation); //Spawn prefabs
Destroy (gameObject);
}
if (WillDrop == false) {
Destroy (gameObject);
}
}
function Score()
{
otherObj[0].GetComponent(ScoreSystem).curScore += 10;
}
function Update()
{
if(isDead == true)
{
Score();
Dead();
}
}
#pragma strict
var Score = 0;
var curScore = 0;
var Style : GUIStyle;
function Update () {
if (curScore > Score)
{
Score++;
}
if (Score > curScore)
{
Score = curScore;
}
}
function OnGUI()
{
GUI.Box(Rect(0,0,100,25), Score.ToString(), Style);
}
Answer by Bunny83 · Nov 02, 2014 at 07:30 PM
To all that answered this question with an advice to use a different GetComponent version, have you actually read the error in the title? If you get an error that "something" is not a member of "somethingElse" that means that "somethingElse" is the problem.
The problem here is that your otherObj variable is of type Array. The Array class is a container class which stores objects without their type. Every item in that collection is stored as System.Object. When you access an element you would need to cast it back to the actual type.
However it's usually better to not use the Array class at all. Either use a built-in array or if the size of your collection will change a lot at runtime, use a generic List. See the Which_Kind_Of_Array_Or_Collection_Should_I_Use wiki page.
This is the correct answer...never use Array. Under any circumstances. It's pointless and should really be removed because it only causes problems like this.
Ha ha - well spotted. We see so many questions with people using GetComponent you tend to reel off the same answer on autopilot. $$anonymous$$y bad. Guess I'll stick to answering questions about C# in the future :)
You can go one step further, as otherObj[0] is the only item ever accessed from the collection, why make otherObj a simple GameObject ins$$anonymous$$d? Change line 5 to:
var otherObj : GameObject;
Answer by 767_2 · Nov 02, 2014 at 02:08 PM
you can do it like
otherObj[0].GetComponent.<ScoreSystem>().curScore += 10;
There's no reason to do that and a couple of reasons not to.
this the generic version of getcomponent in js and it is very much used , and @Eric5h5 i dont know why you say it shouldnt be used
Answer by tanoshimi · Nov 02, 2014 at 02:11 PM
GetComponent returns an object. You need to cast the result to a ScoreSystem before accessing the curScore member (or, better yet, use the generic version of GetComponent).
This is Unityscript, not C#. Don't use the generic version, it's a bit slower and uglier and serves no purpose.
Oh really? Thanks for correction. I'm a C# person so didn't know that! :)
Your answer
Follow this Question
Related Questions
Score System to keep track of Points (Please remove) 1 Answer
Save high score and coins to Space Shooter game 1 Answer
How to save score for survival time? 1 Answer
Score Not Adding Up..? 1 Answer
Scoring System 3 Answers