Question by
Unities · Mar 25, 2016 at 01:07 PM ·
referencesingletonreferencesunassignedreferenceexcept
Unassigned reference exception
In my project I have a singleton class to control the UI in game, when i click a button the it will call a function inside this class to show/hide a panel, but whenever that function it's called I get a error message saying 'unassigned reference' to the panel i'm trying to hide/show, but the thing is everything works fine (at least in the editor). Here's my code:
public IEnumerator PlayAnimation(GameObject obj, GameObject controlButton, float direction, float speed, float delay){
yield return new WaitForSeconds(delay);
if (obj.GetComponent<Animator>() != null) //null reference here
{
obj.GetComponent<Animator>().SetFloat("Direction", direction);
if (direction > 0)
{
obj.GetComponent<Animator>().SetBool("Hide", true);
controlButton.GetComponent<EventTrigger>().enabled = true;
}
else
{
obj.GetComponent<Animator>().SetBool("Hide", false);
controlButton.GetComponent<EventTrigger>().enabled = false;
}
obj.GetComponent<Animator>().speed = speed;
}
}
public void ShowPanel(GameObject panel, GameObject controlButton){
StartCoroutine(uiControl.PlayAnimation(panel, controlButton, -1.0f, 1f, 0f));
HidePanel(panel, controlButton, 3.0f);
}
public void HidePanel(GameObject panel, GameObject controlButton, float delay){
StartCoroutine(uiControl.PlayAnimation(panel, controlButton, 1.0f, 1f, delay));
}
The script of the button trying to call it:
public void OnMouseDown(){ //using EventTrigger
UIControl.uiControl.ShowPanel(UIControl.uiControl.topLeftPanel, UIControl.uiControl.topLeftButton);
}
Comment
Nvm I made a silly mistake of attaching the control script to another game object so I had 2 of them running at the same time