- Home /
Question by
emonegarand · Jan 18, 2014 at 12:56 AM ·
animation2dnullreferenceexceptionanimationclip
Why am I getting a NullReferenceException while trying to set a gameobjects animation frame?
I had been trying to get a score bar made up of sprites to work properly. I have the values converted from my score value into place values for One, Ten, Hundred, Thousand, Ten Thousand, Hundred Thousand and Million all worked out but my script calling for the corresponding GameObject to change its frame to the corresponding frame of animation is giving me a NullReferenceException. How is it not finding the objects I specify? The objects are defined in my gameController script which I have this script referencing.
Heres my script
using UnityEngine;
using System.Collections;
public class ScoreAnimator : MonoBehaviour {
public GameController gameController;
public GameObject scoreTile;
private float frameOne;
private float frameTen;
private float frameHundred;
private float frameThousand;
private float frameTenThousand;
private float frameHundredThousand;
private float frameMillion;
// Use this for initialization
void Start ()
{
GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
if (gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent <GameController>();
}
if (gameController == null)
{
Debug.Log ("Cannot find 'GameController' script");
}
updateFrame ();
frameOne = gameController.one;
frameTen = gameController.ten;
frameHundred = gameController.hundred;
frameThousand = gameController.thousand;
frameTenThousand = gameController.tenthousand;
frameHundredThousand = gameController.hundredthousand;
frameMillion = gameController.million;
}
// Update is called once per frame
void Update ()
{
}
// Update frame of animation for score tiles
void updateFrame ()
{
if (gameObject.name == "One")// Tile for ones place
{
animation["scoreIdle"].time = frameOne; animation["scoreIdle"].speed = 0.0f; animation.Play("scoreIdle");
}
else if (gameObject.name == "Ten")//Tile for tens place
{
animation["scoreIdle"].time = frameTen; animation["scoreIdle"].speed = 0.0f; animation.Play("scoreIdle");
}
else if (gameObject.name == "Hundred")//tile for hundreds place
{
animation["scoreIdle"].time = frameHundred; animation["scoreIdle"].speed = 0.0f; animation.Play("scoreIdle");
}
else if (gameObject.name == "Thousand")//tile for thousands place
{
animation["scoreIdle"].time = frameThousand; animation["scoreIdle"].speed = 0.0f; animation.Play("scoreIdle");
}
else if (gameObject.name == "TenThousand")//tile for ten thousands place
{
animation["scoreIdle"].time = frameTenThousand; animation["scoreIdle"].speed = 0.0f; animation.Play("scoreIdle");
}
else if (gameObject.name == "HundredThousand")//tile for hundred thousands place
{
animation["scoreIdle"].time = frameHundredThousand; animation["scoreIdle"].speed = 0.0f; animation.Play("scoreIdle");
}
else //tile for millions place
animation["scoreIdle"].time = frameMillion; animation["scoreIdle"].speed = 0.0f; animation.Play("scoreIdle");
}
}
Comment