Alan Vieira Rezende
Alan Vieira Rezende

Reputation: 412

Array set initial value problems

I have a script that serves to take or give "life" to the character heart-shaped image.

using UnityEngine;
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;

        }
    }
}

I put as initial 3 hearts, the inspector put size 3:

array 0: 3 hearts

array 1: 2 hearts

array 2: 1 heart

If the player collides with a bomb (prefab) he loses a heart, if he has no heart called the screen "GameOver".

Everything works perfectly fine. But I decided to do a test, i'm creating an "upgrade" if I get a skin for the player, I get along more '2' hearts.

At inspector put size 5

array 0: 5 hearts

array 1: 4 hearts

array 2: 3 hearts

array 3: 2 hearts

array 4: 1 heart

My problem occurs when I put the Script:heartImage.sprite = initialHeart[3]; the game begins with only "3" hearts.

When the player collides with a bomb (prefab), he gains a life rather than lose one life, but the following collisions it begins to lose life normally until the game is over.

If I start with 3 hearts and "caught" an extra heart in the game does not change, rather than up 3 to 4, I still remain with 3 hearts.

After I collide with the first bomb (prefab) and gain 1 heart instead of losing, the script starts running normally. If you collide with the bomb, i lose, if caught an extra heart i win.

I want to know why when I change the heartImage.sprite = initialHeart[0]; (5 hearts) to heartImage.sprite = initialHeart[2]; (3 hearts), appears this bug in the first collision.

Edit

I found where the problem but do not know how to solve it.

My array will be fixed 5, but sometimes I want to start [0] (5 hearts) and other situations that start the [2] (3 hearts).

Inside the Start (), declared heartImage.sprite = initialHeart [2]; (initial 3 hearts) . But within the TakeHeart method has the function that takes Hearts.

if (currentHearts <(hearts - 1))
{
currentHearts + = 1;
heartImage.sprite = initialHeart [currentHearts]; ;
return true;
}

It turns out that when I declare that: heartImage.sprite = initialHeart [currentHearts];

I'm saying initialHeart[1] (4 hearts), but should be [3] (2 hearts)

It is possible to do something to add: initialHeart [currentHearts] + initialHeart [2]; `?

Thus the result of intialHeart would [3] (2 hearts), the way I want.

Upvotes: 0

Views: 94

Answers (2)

Iury Dias
Iury Dias

Reputation: 315

Try defining currentHearts as 2 in the Start method... When you add 1 to it, it'll be 3 and you'll access the index 3 of the array.

currentHearts = 2;

Upvotes: 1

Gunnar B.
Gunnar B.

Reputation: 2989

If you increase the size of your array, but don't change your other code to incorporate that you get those problems. Make your code size independent by not using fixed indices, at least for the upper range. 0 can of course stay, but your upper end should be something like initialHeart[initialHeart.Length].

If you want to change the size of the array on runtime, you need to change you array to some other container that can be resized dynamically like List<>. For this you can work with .Count instead of .Length and there is also a .Last() for getting the last element a little shorter.

Upvotes: 2

Related Questions