I just started to make a merge game. I try to merge 2 objects but because the script is in both of the objects it repeats the script 2 time and it spawn 2 objects instead of 1. Here is the code.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class merge : MonoBehaviour
{
public GameObject box2Prefab;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.transform.tag == "box1")
{
GameObject box2z = Instantiate(box2Prefab);
box2z.transform.parent = transform.parent;
box2z.transform.position = this.transform.position;
Destroy(other.gameObject);
Destroy(gameObject);
}
}
}
I cant delete the script on the other object because if I put a spawner it won’t work.