if + else if statements not working (i'm a beginner!)

Hi there,

Hope you’re all great, I’m just looking for a bit of help with the below code.

I’m practicing making a Block Breaker type game, in the game the blocks that you break with a ball need to be hit twice in total to be destroyed. I have the sprites for the box and the damaged box stored in the boxState array, at the moment each ball hit is only adjusting the sprite and is never calling the “else if” statement to destroy the object on the second hit.

I seem to be having trouble with the first if statement always returning false (I think), but my thinking was that on the first hit, the boxState would change to [1] in the array and therefore make the first if statement false on a repeat hit.

Anyone have any ideas?

You’re all great :slight_smile:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Box : MonoBehaviour
{

    [SerializeField] Sprite[] boxState;

    Sprite sprite;
    SpriteRenderer spriteRenderer;

    private void OnCollisionEnter2D(Collision2D collision)
    {

        spriteRenderer = GetComponent<SpriteRenderer>();

        if (collision.gameObject.name == ("ball") && (sprite = boxState[0]))
        {
            sprite = boxState[1];
            CallNewSprite();
            Debug.Log(sprite.name);
        }

        else if (collision.gameObject.name == ("ball") && (sprite = boxState[1]))
        {
            Destroy(gameObject);
        }
    }

    private void CallNewSprite ()
    {
        spriteRenderer.sprite = sprite;
    }
}

a single = is an assignment. You need == on the second parts of your if statements as well:

if (collision.gameObject.name == ("ball") && (sprite == boxState[0]))

and

else if (collision.gameObject.name == ("ball") && (sprite == boxState[1]))
2 Likes

Thanks so much for the reply.

This is stopping any changes at all now though, without it, the sprite does change to the boxState[1], but with your changes the collision isn’t triggering any changes?

Then the second part of your if statement isn´t true i guess.

put a: Debug.Log(sprite == boxState[1])); on line 17 to see if that part is true

and you could debug it to see, what´s wrong. maybe the sprites are different objects

1 Like

where do you initialize the “sprite” variable ?

by default its value will be null until you assign it something.

Try checking out the debugger with visual studio, fairly simple to get started on tiny scripts like that and simply hovering variables while code is executing this should show you the problem quickly !

1 Like

I didn’t assign sprite in the script but added the same boxState[0] sprite into the object as default, this was already debugging as true. + On the first if statement, it’s also changing sprites fine, it’s just the second Destroy that seems to be an issue.

I’ll have a look at Visual Studio’s debugger now. Thanks for that :slight_smile:

1 Like

Yeah that’s reporting as true for [0] for the first sprite and [1] for the “damaged” one, very strange! We’ll get there.

So you get true for both ?

Or does the first one work but the second still wrong ?

1 Like

True for both for sure. In gameplay, on the first hit boxState[0] is true and on the second hit boxState[1] is true, boxState[0] being no longer true after the sprite changes.

but for whatever reason, this else if statement isn’t destroying the object :frowning:

 else if (collision.gameObject.name == ("ball") && (boxState[1]))
        {
            Destroy(gameObject);
        }

Sorry to post twice but if I change both statements to if as below, I’m destroying the objects?

So, the issue is that the first statement is always returning true even following the boxState[1] change, stopping the Destroy method from ever being called due to it being on an else if… Contradicts my Debug but I’ll keep nosing!

        if (collision.gameObject.name == ("ball") && (boxState[0]))
        {
            sprite = boxState[1];
            CallNewSprite();
        }

        if (collision.gameObject.name == ("ball") && (boxState[1]))
        {
            Destroy(gameObject);
        }

Fixed!

  • I called a Start method to assign boxState[0] to the sprite variable as per @_met44 suggestion.
  • I then noticed on my if statements that I’d changed (sprite == boxstate[0]) to (boxState[1]) whilst debugging and trying some fixes. Bringing sprite == back pushed through a fix here.

Thanks so much guys!

public class Box : MonoBehaviour
{

    [SerializeField] Sprite[] boxState;

    Sprite sprite;
    SpriteRenderer spriteRenderer;

    private void Start()
    {
        sprite = boxState[0];
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {

        spriteRenderer = GetComponent<SpriteRenderer>();

        Debug.Log(sprite == boxState[0]);


        if (collision.gameObject.name == ("ball") && (sprite == boxState[0]))
        {
            sprite = boxState[1];
            CallNewSprite();
        }

        else if (collision.gameObject.name == ("ball") && (sprite == boxState[1]))
        {
            Destroy(gameObject);
        }
    }

    private void CallNewSprite()
    {
        spriteRenderer.sprite = sprite;
    }
}
1 Like