Question by
TimmyRB · Dec 12, 2016 at 09:34 PM ·
c#unity 5getcomponentcounterscoreboard
Unity Scoring Counter Error
Hey everyone, I'm trying to get a scoreboard working in my game by having a score keeper store and print vars to the console (For Now) and whenever the code tries to add to the integers "blueScore" and "redScore" I get this error "NullReferenceException: Object Reference not set to an Instance of an object..." Anyone have an idea why this is happening?
void Update () {
if (thisObject.position.y < deathHeight)
{
if (randomRespawn)
{
rangeX = Random.Range(minX, maxX);
rangeY = Random.Range(minY, maxY);
rangeZ = Random.Range(minZ, maxZ);
respawnPosition = new Vector3(rangeX, rangeY, rangeZ);
}
rigidObject.velocity = new Vector3(0, 0, 0);
thisObject.position = respawnPosition;
if (this.name == "Player")
{
GetComponent<scoreKeeper>().redScore++;
}
if (this.name == "Enemy")
{
GetComponent<scoreKeeper>().blueScore++;
}
}
Comment
Answer by sisse008 · Dec 13, 2016 at 01:07 AM
does your code enter the loop if (this.name == "Player") and if (this.name == "Enemy")? if it does, show us your scoreKeeper.c
I dont believe score$$anonymous$$eeper is a unity defined component. you might have a component you name scorekeeper and the component might be a GUItext... so you would do:
GUItext scorekeeper = GetComponent<GUItext>( );
int score;
scorekeeper.text = "Score: " + score;