I have and script below that does the following:
The player starts with 3 hearts [0] if it suffers damage loses one heart and changes the texture to 2 hearts 1, if he wins one extra heart back to the texture [0] .. If you're having only one heart [ 2] and suffers an injury, the game ends.
This code works perfectly, but I would like to convert it to UI Canvas Image. How do I do this?
using UnityEngine;
using System.Collections;
public class Hearts : MonoBehaviour {
public Texture2D[]initialHeart;
private int hearts;
private int currentHearts;
// Use this for initialization
void Start () {
GetComponent<GUITexture>().texture = initialHeart[0];
hearts = initialHeart.Length;
}
// Update is called once per frame
void Update () {
}
public bool TakeHeart()
{
if (hearts < 0) {
return false;
}
if (currentHearts < (hearts - 1)) {
currentHearts += 1;
GetComponent<GUITexture> ().texture = initialHeart [currentHearts];
return true;
} else {
return false;
}
}
public bool AddHeart() {
if (currentHearts > 0) {
currentHearts -= 1;
GetComponent<GUITexture> ().texture = initialHeart [currentHearts];
return true;
} else {
return false;
}
}
}
Edit:
Look what is happening:
In the object inspector, HealthBar I put 3 hearts as image (first imagem attached). But when I go into the script and try to select the images to be changed according to the user's progress, is not allowed to use image, objects only (second imagem attached).
1.Change Texture2D
to Sprite
.
2.Then change GetComponent<GUITexture>().texture = initialHeart[0];
to GetComponent<Image>().sprite = initialHeart[0];
3.And the two GetComponent<GUITexture> ().texture = initialHeart [currentHearts];
to GetComponent<Image>().sprite = initialHeart[currentHearts];
Note: Doing GetComponent<GUITexture> ().texture
and GetComponent<Image>().sprite
is not good. If you need to access any component multiple times, you need to cache
it. That means you do GetComponent<Image>()
once and save it to variable. That variable you can now use many other times. I noticed this in your other questions/codes too and decided it's time to tell you that. Take a look at the code below to understand it more. Code not tested but it compiles.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Hearts : MonoBehaviour
{
public Sprite[] initialHeart;
private int hearts;
private int currentHearts;
// Use this for initialization
//Used for chaching Image so that you won't be doing GetComponent<Image>(); each time
Image heartImage;
void Start()
{
//Cache the Image once so that you won't be doing GetComponent<Image>(); each time
heartImage = GetComponent<Image>();
heartImage.sprite = initialHeart[0];
hearts = initialHeart.Length;
}
// Update is called once per frame
void Update()
{
}
public bool TakeHeart()
{
if (hearts < 0)
{
return false;
}
if (currentHearts < (hearts - 1))
{
currentHearts += 1;
heartImage.sprite = initialHeart[currentHearts];
return true;
}
else
{
return false;
}
}
public bool AddHeart()
{
if (currentHearts > 0)
{
currentHearts -= 1;
heartImage.sprite = initialHeart[currentHearts];
return true;
}
else
{
return false;
}
}
}
EDIT: I change changed the initialHeart
from Image
to Sprite
so that you can put Sprite
directly. Don't forget to remove GUITexture
from the GameObject this script will be attached to. Also make sure that the GameObject you are attaching this script to has an Image
attached to it. If you don't, GetComponent<Image>();
will fail.