Set public Image in canvas from script
Hi guys, I've got an health system that worked fine in my level 1 : my HUD script is searching for the level manager and see how many lives the player's got : depending on this "int" the hud display a certain sprite of an image, image contained by the canvas attached to MainCamera. I can assign this image by drag n drop but as I want to "dontdestroyonload" my player and his scripts, the HUD really needs to find the image by himself and i Just can't make it work...
Here are some pictures to help you visualize my problem.
Now my script looks like this : [CODE] using UnityEngine; using System.Collections; using UnityEngine.UI; public class HUD : MonoBehaviour {
public Sprite[] LivesSprites;
public Sprite[] BeuteSprites;
public Image LifeBeuteUI;
public Image LivesUI;
private LevelManager player;
public Health health;
void Start() {
player = FindObjectOfType<LevelManager>();
health = GetComponent<Health> ();
Image LivesUI = GameObject.FindWithTag("Lives").GetComponent<Image> ();
}
void Update () {
LivesUI.sprite = LivesSprites[player.lives-1];
LifeBeuteUI.sprite = BeuteSprites[health.currentHealth-1];
}
} [CODE/]
I tried to get the image by .find("name") and all the calls I know of, and I just can't get it right. I'm sure I'm a noob on this one but any help would be really appreciate ! :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612034700im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GetComponent is not a member of Object 0 Answers
How do declare a nonspecific class to be defined later. 1 Answer
Referencing variable from another script on another object 3 Answers
GetComponent returns null 0 Answers
Get Vector3 From Another Script 1 Answer