How to Change Sprite or Image on Canvas when Losing Health?
Hi, my first time posting here. Basically I want the sprite or Image to change on the Canvas when the player loses health. I have got no idea how to do this after reading everywhere, maybe I just didn't understand what was explained. So: playerHealth = 3 then "Render" Sprite hearts3. playerHealth = 2 then "Render" Sprite hearts2. and so on. I hope this is clear :) I also want the hearts Image and player health to carry on to the next scene as well, because Im planning to make more Levels. thanks for your help!! Sorry for the noob code :P
![alt text][2]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameOver : MonoBehaviour
{
public int playerHealth = 3;
public GameObject ballPrefab;
public Image hearts3;
public Image hearts2;
public Image hearts1;
public Image hearts0;
//UI.Image
void Start()
{
}
public void Restart()
{
SceneManager.LoadScene("Level1");
}
private void OnTriggerEnter2D(Collider2D other)
{
playerHealth -= 1;
Instantiate(ballPrefab, new Vector3(0.2f, -85f, 3f), Quaternion.identity);
}
void Update()
{
if (playerHealth == 3)
{
//Display hearts3 Image.
Debug.Log("Life3");
}
if (playerHealth <= 2)
{
//Display hearts2 Image.
Debug.Log("Life2");
}
if (playerHealth <= 1)
{
//Display hearts1 Image.
Debug.Log("Life1");
}
if (playerHealth <= 0)
{
//Display hearts0 Image.
GameMatch();
}
}
void GameMatch()
{
SceneManager.LoadScene("GameOver");
}
} [2]: /storage/temp/156179-image1.png
Answer by Noxi027 · Apr 09, 2020 at 07:23 PM
Thanks for all your advice @streeetwalker ! After a lot of fiddling with the code and reading up I found the 127 problems and then eventually the solution! :D
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameOver : MonoBehaviour
{
public int playerHealth = 3;
public Image healthImage;
public GameObject ballPrefab;
public Sprite hearts0;
public Sprite hearts1;
public Sprite hearts2;
public Sprite hearts3;
//UI.Image
void Start()
{
}
public void Restart()
{
SceneManager.LoadScene("Level1");
}
private void OnTriggerEnter2D(Collider2D other)
{
playerHealth -= 1;
Instantiate(ballPrefab, new Vector3(0.2f, -85f, 3f), Quaternion.identity);
}
void Update()
{
if (playerHealth == 3)
{
healthImage.sprite = hearts3;
Debug.Log("Life3");
}
if (playerHealth <= 2)
{
healthImage.sprite = hearts2;
Debug.Log("Life2");
}
if (playerHealth <= 1)
{
healthImage.sprite = hearts1;
Debug.Log("Life1");
}
if (playerHealth <= 0)
{
healthImage.sprite = hearts0;
GameMatch();
}
}
void GameMatch()
{
SceneManager.LoadScene("GameOver");
}
}
Answer by streeetwalker · Apr 09, 2020 at 09:28 AM
@Noxi027, you need a reference to the UI element's Image component of your Health object. At the top of your code, create a public Image variable to hold that, and then drag your Health object to that field in the inspector.
For example call it 'healthImage'. Then in your code, you can set healthImage.image = hearts2.image and so on, because it is the image property of an Image object that contains the reference to the actual texture. (see the unity documentation on the Image class ) I believe that will do it.
Your answer
Follow this Question
Related Questions
Wobbly images on Canvas 0 Answers
[Help]Randomly Selecting a Background Image from an Array of Backgrounds 1 Answer
Unable to make animations for UI Image 2 Answers
Need help with shield bar enable and disable. 0 Answers
How to set canvas image to a Texture2D created in a script at runtime? 1 Answer