Unity - How to make two of the same GameObjects collide, destroy each other and create a new GameObject in the target position?

So, I want the two balls to collide, destroy themselves, and then another ball appears in their place (preferably at a certain speed). However, when I try to attach a script to the ball, both instances of the ball are destroyed on contact, and then immediately generate two national balls, since both of them have a code. This makes both balls appear and destroy each other again and again. I have this script attached to balls:

private Vector3 ballPosition;

void OnTriggerEnter2D (Collider2D other) {

    if (other.gameObject.tag == "Ball") {

        ballPosition = new Vector3 ((transform.position.x + other.transform.position.x) / 2, (transform.position.y + other.transform.position.y) / 2, 0.0f);
        StartCoroutine ("RespawnBall");
    }
}

IEnumerator RespawnBall () {

    Instantiate (gameObject, ballPosition, Quaternion.identity);
    Destroy (gameObject);
    yield return null;
}

How can I make this code destroy both balls and then spawn only one instance of the collection?

+4
source share
3

script, , , OnTriggerEnter2D(), :

private Vector3 ballPosition;
public bool spawnNewBall;

void Start() {
    spawnNewBall = true;
}

void OnTriggerEnter2D (Collider2D other) {

    if (other.gameObject.tag == "Ball") {

        if (spawnNewBall) {
            other.GetComponent</*YourScriptName*/>().spawnNewBall = false;
        }

        ballPosition = new Vector3 ((transform.position.x + other.transform.position.x) / 2, (transform.position.y + other.transform.position.y) / 2, 0.0f);
        StartCoroutine ("RespawnBall");
    }
}

IEnumerator RespawnBall () {
    if (spawnNewBall) {
        Instantiate (gameObject, ballPosition, Quaternion.identity);
    }
    Destroy (gameObject);
    yield return null;
}

, :

public class TwoToOne : MonoBehaviour {

    public bool doNothing;

    void OnCollisionEnter (Collision col)
        {
        if (doNothing) return;

        col.gameObject.GetComponent<TwoToOne>().doNothing = true;
        Destroy(col.gameObject);

        GameObject newCube = Instantiate(gameObject);

        Destroy(gameObject);
        }

    }

script Unity.

, " "; , . Unity :

  • A, "" - . DestroyImmediate(col.gameObject) Unity.

  • B, "" script . col.gameObject.GetComponent<TwoToOne>().enabled = false Unity

  • C, "flag" ( ) - .

, Unity A B, C. B - , Unity5, C!

+1

@JoeBlow, UnityEvents .

.

2 . , ( ), - , script .


EventManager.cs

using UnityEngine;
using System.Collections;

public static class EventManager{

    // Create our delegate with expected params.
    // NOTE params must match SphereScript.PostCollision declaration
    public delegate void CollideEvent(string message);

    // Create the delegate instance. This is the one we will invoke.
    public static event CollideEvent PostCollision;

    // Called whenever an object has collided with another
    public static void Collision(GameObject obj1, GameObject obj2, Vector3 collisionPoint){
        if (obj1.GetComponent<sphereScript>().isAlive && obj2.GetComponent<sphereScript>().isAlive) {

            //Kill the 2 objects which haev collided.
            obj1.GetComponent<sphereScript> ().Kill ();
            obj2.GetComponent<sphereScript> ().Kill ();

            //Create a cube.
            GameObject cube = GameObject.CreatePrimitive (PrimitiveType.Cube);
            cube.transform.position = collisionPoint;

            // Invoke delegate invocation list
            PostCollision("Something is dead");
        }
    }
}

SphereScript.cs

using UnityEngine;
using System.Collections;

public class sphereScript : MonoBehaviour {
    // Am I alive?
    public bool isAlive;

    // Use this for initialization
    void Start () {
        // Add a function we want to be called when the EventManager invokes PostCollision
        EventManager.PostCollision += PostCollision;
        isAlive = true;
    }

    // Update is called once per frame
    void Update () {

    }

    //Invoked from EventManager.PostCollision delegate
    void PostCollision(string message){
        if(isAlive)
            Debug.Log (this.name + " message received: " + message);
    }

    // Called when it is time to destroy this gameobject
    public void Kill(){
        isAlive = false;
        Destroy (this.gameObject);
    }

    //On collision with another object
    void OnCollisionEnter2D(Collision2D collision){
        if (collision.gameObject.GetComponent<sphereScript>()) {
            EventManager.Collision (this.gameObject, collision.gameObject, collision.contacts [0].point);
        }
    }

    // Called after this object has been destroyed
    void OnDestroy(){
        // cleanup events for performance.
        EventManager.PostCollision -= PostCollision;
    }
}

?

"" . . EventManager , .

0

, , script, , 2- . , . , , , ,

-2

Source: https://habr.com/ru/post/1662360/


All Articles