Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Noxi027 · Apr 09, 2020 at 09:14 AM · canvasimagespriterendererhealth

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


alt text

image1.png (142.1 kB)
image1.png (142.1 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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");
     }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

209 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges