I have here a peculiar problem. I have a multi-character game, and a function in my game controller to switch between said caharcters. To do so, my HUD must update. I have sub-HUDs on the top, each displaying the health, stamina and a sprite for the character. On the bottom, a bigger HUD to display the health, stamina and pic of the “active” character. When I switch character, my HUDBehaviour.cs script copies the info from the sub-HUD given in argument in the setBottomHUD method. This fuction works perfectly except on the first frame, where it cannot find the sprite, and returns an error (it gets the health and stamina fine though…).
The weirdest part is what happened when debugging the program. I just inserted a Debug.Log(“OK”); line in the script, and it wouldn’t log anything if placed AFTER the sprite loading line, as if the script just… stopped? Once again, switching characters afterwards works fine, but the first frame bugs out. Here is my HUDBehaviour.cs script :
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class HUDBehaviour : MonoBehaviour
{
private Slider displayedStamina;
private Slider displayedHealth;
private Slider stamina;
private Slider health;
private Image pic;
void Start()
{
displayedStamina = transform.Find("StaminaSlider").GetComponent<Slider>();
displayedHealth = transform.Find("HealthSlider").GetComponent<Slider>();
pic = transform.Find("Image").GetComponent<Image>();
}
// Update is called once per frame
void Update ()
{
displayedStamina.value = stamina.value;
displayedHealth.value = health.value;
}
public void setBottomHUD(GameObject sH)
{
pic.sprite = sH.transform.Find("Image").GetComponent<Image>().sprite;
stamina = sH.transform.Find("StaminaSlider").GetComponent<Slider>();
health = sH.transform.Find("HealthSlider").GetComponent<Slider>();
}
}
The game controller calls setBottomHUD at it’s start, with a predefined sub-HUD argument, FYI.
Thanks to anyone who can answer this!