Question by
david_kuehn · Jul 15, 2016 at 04:16 PM ·
erroranimatorinspectorunassignedreferenceexceptunassiunassignedreferenceexception
Unassigned Reference Exception on an Animator, but Animator is assigned in Inspector?
I'm getting an Unassigned Reference Exception in my script that references an Animator component. I assign the Animator to the script in the inspector, but it still returns the error.
Here is the script.
using UnityEngine;
using UnityEngine.UI;
public class Currency : MonoBehaviour {
public int startCoins;
public int goldCoins;
public DaylightCycle daylightCycle;
private int lastDayCounted = 0;
private int enemyDominions;
private int lastEnemyDominionsCounted = 8;
private int lastCoinCount;
private bool wasProductive;
[SerializeField]
private Animator coinsAddedAnimator;
public Text coinsAddedText;
void Start()
{
goldCoins = startCoins;
lastCoinCount = startCoins;
}
void Update()
{
//If it is the next day
if (daylightCycle.dayCounter > lastDayCounted)
{
if (lastCoinCount + 200 <= goldCoins)
{
wasProductive = true;
}
if (wasProductive)
{
//Give Player 10 more Gold Coins
DistributeCoins(20);
}
lastCoinCount = goldCoins;
//Make lastDayCounted the current day
lastDayCounted = daylightCycle.dayCounter;
}
enemyDominions = GameObject.FindGameObjectsWithTag("EnemyDominion").Length;
if (enemyDominions < lastEnemyDominionsCounted)
{
DistributeCoins(50);
lastEnemyDominionsCounted = enemyDominions;
}
}
public void DistributeCoins(int _amount)
{
goldCoins += _amount;
coinsAddedText.text = "+" + _amount;
coinsAddedAnimator.SetTrigger("CoinsWereAdded");
}
}
And here is the error.
capture1.png
(4.8 kB)
capture.png
(14.0 kB)
Comment