Ricardo Andrés
Ricardo Andrés

Reputation: 11

Can I have a series of if/else if statements inside one OnCollisionEnter2D function?

I'm currently creating a pong game in which the moment the ball hits one of the pong paddles, it splits in two. I do this by destroying the paddle that receives the collision, and instating a split paddle that I've made a prefab.

My issue is every prefab is tagged differently and every time the ball hits a paddle, it should detct that tag and do something... but after the first split, once the new paddle is instantiated, the function doesn't fire...

Can I have several if/else if statements like this? What am I missing?

Here is my code:

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

public class PaddleSplit_Script : MonoBehaviour
{
    public GameObject split_paddle1;
    public GameObject split_paddle2;
    public GameObject split_paddle3;

    public GameObject split_opponent_paddle1;
    public GameObject split_opponent_paddle2;
    public GameObject split_opponent_paddle3;
    //public override void Apply(GameObject target)
    //{
    //    void 

    //    if (target.gameObject.CompareTag("Player 1"))
    //    {

    //        //Instantiate()
    //    }
    //}

    private void OnCollisionEnter2D(Collision2D collision)
    {
        // Pre-State
        if (collision.gameObject.CompareTag("Player 1"))
        {
            Debug.Log("Player Split");
            Destroy(collision.gameObject);
            Instantiate(split_paddle1);
            //Destroy(gameObject);

        }

        else if (collision.gameObject.CompareTag("Player 2"))
        {
            Debug.Log("Opponent Split");
            Destroy(collision.gameObject);
            Instantiate(split_opponent_paddle1);
            //Destroy(gameObject);
        }

        // Primary State

        else if (collision.gameObject.CompareTag("Player 1_1"))
        {
            Debug.Log("Player split again");
            Destroy(collision.gameObject);
            Instantiate(split_paddle2);
        }

        else if (collision.gameObject.CompareTag("Player 2_1"))
        {
            Debug.Log("Opponent split again");
            Destroy(collision.gameObject);
            Instantiate(split_opponent_paddle2);
        }


        // Secondary State

        // else if (collision.gameObject.CompareTag("Player 1_2"))
        // {
        //     Destroy(collision.gameObject);
        //     Instantiate(split_paddle3);
        // }

        // else if (collision.gameObject.CompareTag("Player 2_2"))
        // {
        //     Destroy(collision.gameObject);
        //     Instantiate(split_opponent_paddle3);
        // }
    }
}


As you'll notice, I broke them down in states (pre-state being the paddle un-split).

What I am trying to accomplish is once the ball hits a paddle, it should detect that collision based off the tag it hits....

Upvotes: 0

Views: 73

Answers (1)

derHugo
derHugo

Reputation: 90659

Can I have several if/else if statements like this?

Of course you can! OnCollisionEnter2D (in this regard) is a c# method like any other.

Should you? That's another question.

Unfortunately the tag system is quite limited there and not really dynamically extendable. Also you might just miss to tag and referene your objects correctly.

Instead of using tags in this case I would rather go with a simple component like e.g.

public enum Side
{
    Player,
    Opponent
}

public class Paddle : Monobehaviour
{
    public int splitLevel;
    public Side side;
}

and do e.g.

public Paddle[] paddlesPrefabs;

private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.TryGetComponent<Paddle>(out var paddle))
    {
        var currentSplitLevel = paddle.splitLevel;
        var side = paddle.Side;

        var newSplitLevel = currentSplitLevel + 1;

        if(newSplitLevel >= paddlesPrefabs.Length)
        {
            // GameOverAndLoserIs(side);
        }
        else
        {
            var newPrefab = paddlesPrefabs[newSplitLevel];
            var newPaddle = Instantiate(newPrefab);
            newPaddle.splitLevel = newSplitLevel;
            newPaddle.side = side;
        }
    }
}

Upvotes: 0

Related Questions