RAXIC
RAXIC

Reputation: 11

I am developing a Merge kind game. But Collide Problem?

I am developing a Merge kind game. When these two objects collide, one object has to spawned. But in my code, I got two objects spawning. Do you have any idea about this situation or do you have an Algorithm for Merge games? Thank you.

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

public class Merge : MonoBehaviour {

public GameObject Circle;
public GameObject Heptagon;
public GameObject Hexagon;
public GameObject Octagon;
public GameObject Pentagon;
public GameObject Square;
public GameObject Triangle;


void start()
{
    
}

void update()
{
   
}

public void OnCollisionEnter2D(Collision2D collision)
{

    if (collision.gameObject.tag == "Triangle")
    {
        Debug.Log("Merged !!!");
        Instantiate(Circle, new Vector2(Triangle.transform.position.x, transform.position.y), Quaternion.identity);
        Destroy(Triangle);
    }

    else if (collision.gameObject.tag == "Circle")
    {
        Debug.Log("Merged !!!");
        Instantiate(Square, new Vector2(Circle.transform.position.x, Circle.transform.position.y), Quaternion.identity);
        Destroy(Circle);


    }


    else if (collision.gameObject.tag == "Square")
    {
        Debug.Log("Merged !!!");
        Instantiate(Pentagon, new Vector2(Square.transform.position.x, Square.transform.position.y), Quaternion.identity);
        Destroy(Square);


    }

    else if (collision.gameObject.tag == "Pentagon")
    {
        Debug.Log("Merged !!!");
        Instantiate(Hexagon, new Vector2(Pentagon.transform.position.x, Pentagon.transform.position.y), Quaternion.identity);
        Destroy(Pentagon);


    }

    else if (collision.gameObject.tag == "Hexagon")
    {
        Debug.Log("Merged !!!");
        Instantiate(Heptagon, new Vector2(Hexagon.transform.position.x, Hexagon.transform.position.y), Quaternion.identity);
        Destroy(Hexagon);


    }


    else if (collision.gameObject.tag == "Heptagon")
    {
        Debug.Log("Merged !!!");
        Instantiate(Octagon, new Vector2(Heptagon.transform.position.x, Heptagon.transform.position.y), Quaternion.identity);
        Destroy(Heptagon);
    }


}

} `

Upvotes: 1

Views: 69

Answers (1)

derHugo
derHugo

Reputation: 90580

Well your issue should be clear: You have two objects with this script colliding

=> On both components OnCollisionEnter2D is called.

Additionally if you place a new object with this same component on the same spot then OnCollisonEnter2D might be called again for this new object right away again!

I don't think you wanted to

Destroy(Triangle);

etc either => You are trying to destroy prefabs.

I think your code should probably rather be e.g.

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

public class Merge : MonoBehaviour 
{
    public GameObject Circle;
    public GameObject Heptagon;
    public GameObject Hexagon;
    public GameObject Octagon;
    public GameObject Pentagon;
    public GameObject Square;
    public GameObject Triangle;
    
    //NOTE: MonoBehaviour Messages like Start and Update are called even if they are empty
    //NOTE: Thus only causing unnecessary overhead => If you don't need them always remove them
    
    public void OnCollisionEnter2D(Collision2D collision)
    {
        var otherTransform = collision.transform;
        var otherObject = collision.gameObject;
    
        // In general rather use CompareTag instead of ==
        if (otherObject.CompareTag("Triangle"))
        {
            Debug.Log("Merged !!!");
            // Destroy the object we collide with
            Destroy(otherObject);
            // and our self
            Destroy(gameObject);
            // Then spawn the new object in the position of the hit object
            Instantiate(Circle, (Vector2)otherTransform.position, Quaternion.identity);
        }
        else if (otherObject.CompareTag("Circle"))
        {
            Debug.Log("Merged !!!");
            Destroy(otherObject);
            Destroy(gameObject);
            Instantiate(Square, (Vector2)otherTransform.position, Quaternion.identity);
        }
        else if (otherObject.CompareTag("Square"))
        {
            Debug.Log("Merged !!!");
            Destroy(otherObject);
            Destroy(gameObject);
            Instantiate(Pentagon, (Vector2)otherTransform.position, Quaternion.identity);
        }
        else if (otherObject.CompareTag("Pentagon"))
        {
            Debug.Log("Merged !!!");
            Destroy(otherObject);
            Destroy(gameObject);
            Instantiate(Hexagon, (Vector2)otherTransform.position, Quaternion.identity);
        }
        else if (otherObject.CompareTag("Hexagon"))
        {
            Debug.Log("Merged !!!");
            Destroy(otherObject);
            Destroy(gameObject);
            Instantiate(Heptagon, (Vector2)otherTransform.position, Quaternion.identity);
        }
        else if (otherObject.CompareTag("Heptagon"))
        {
            Debug.Log("Merged !!!");
            Destroy(otherObject);
            Destroy(gameObject);
            Instantiate(Octagon, (Vector2)otherTransform.position, Quaternion.identity);
        }
    }   
}

After destroying the hit object and yourself before instantiating the new object it should be save that only this one collision is handled.

Upvotes: 0

Related Questions