0
\$\begingroup\$

Was following the tutorial in this video https://oxmond.com/how-to-make-multiple-copies-of-a-gameobject/ on how to duplicate object. I want to modify my code I learned from this tutorial https://www.youtube.com/watch?v=OR0e-1UBEOU&t=4178s so that the bird will increase its size proportion to the distance travel and when scale>certain value, it break into 2 identical bird object that possess the same properties as the first one. I already did the part size increase in proportion to the distance travel. Now I create an empty game object and attach the script "explode" together with prefab of the bird object to it but how to write the proper code to carry out the function if the main bird object is destroyed then it run the code "explode" to duplicate bird?

   using UnityEngine;
   using UnityEngine.Assertions.Must;
   using UnityEngine.SceneManagement;

   public class Bird : MonoBehaviour

{Vector3 OldPosition;
Vector3 DistanceDifference;
float TotalDistance = 0;
Vector3 _initialPosition;
private bool _birdWasLaunched;
[SerializeField] private float _launchPower = 500;

void start()
{ 
    OldPosition = transform.position;
    
}



private void Awake()
{
    _initialPosition = transform.position;

}


    void Update()
    {   

   
    if (_birdWasLaunched &&
             GetComponent<Rigidbody2D>().velocity.magnitude > 0.1)
    {
        DistanceDifference = transform.position - OldPosition;
        TotalDistance += DistanceDifference.magnitude;
        OldPosition = transform.position;
        transform.localScale = new Vector3(0.2f * Mathf.Abs((TotalDistance * 0.2f)), 0.2f *Mathf.Abs(TotalDistance * 0.2f) , TotalDistance*0.5f);

    }

    if (transform.position.y > 20.00 ||
        transform.position.y < -20.00 || transform.position.x > 23 || transform.position.x < -23)
    {
        string currentSceneName = SceneManager.GetActiveScene().name;
        SceneManager.LoadScene(currentSceneName);
        

    }
    }



void OnMouseDown()
{
    GetComponent<SpriteRenderer>().color = Color.red;


}



private void OnMouseUp()
{
    GetComponent<SpriteRenderer>().color = Color.white;
    Vector2 directionToInitialPosition = _initialPosition - transform.position;
    GetComponent<Rigidbody2D>().AddForce(directionToInitialPosition * _launchPower);
    GetComponent<Rigidbody2D>().gravityScale = 1;
    _birdWasLaunched = true;


}

private void OnMouseDrag()
{
    Vector3 newPosition =    Camera.main.ScreenToWorldPoint(Input.mousePosition);
    transform.position = new Vector3(newPosition.x, newPosition.y);

This the 2nd script to duplicate the bird but i'm not sure what the correct codes I have to put in if the 1st game object destroy, then it duplicate 2 bird?

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

public class Explode : MonoBehaviour
{
 public GameObject Bird;

// Update is called once per frame
void Update()
{   
    (if Destroy(Bird))
    GameObject BirdClone = Instantiate(Bird);
}
}
\$\endgroup\$
1
  • \$\begingroup\$ if (Destroy(Bird)) is not going to work. It executes the operation to destroy the bird gameObject, and then decides what to do depending on the result. But the Destroy method does not return a result, so that code is syntactically incorrect. When you want to do something on the destruction of an object, use the OnDestroy event method. \$\endgroup\$ – Philipp Nov 2 '20 at 9:33
1
\$\begingroup\$

Your approach is not going to work. You can not duplicate an object after it was destroyed, because it doesn't exist anymore at that time.

When the original object was instantiated from a prefab, then you could instantiate that prefab again when the object gets destroyed. You could do that in the OnDestroy method of a script which is on the gameObject you want to duplicate on destruction. That script does of course need a reference to the original prefab. But make sure that the children don't also inherit the duplicate-on-death behavior. Otherwise the number of objects will grow exponentially until your game runs out of RAM and crashes. You can remove a script from an object by passing just that component to Destroy.

When you are not working with prefabs, then you could handle this in the script which causes the destruction of the gameObject (which appears to not be included in the question). Just Instantiate two new instances before you Destroy the old one.

\$\endgroup\$
5
  • \$\begingroup\$ Thank again. This looks simpler. I'll try this out, this method is actually what I'm looking for, on destroy split into 2 new objects, then after the 2 objects destroyed split it into 4 new objects and so on. I just want to simulate the result and check how it going only. \$\endgroup\$ – chuackt Nov 2 '20 at 9:40
  • \$\begingroup\$ @chuackt And then into 8, 16, 32, 64, 128, 256, 512, 1024, 2048... When does it stop? \$\endgroup\$ – Philipp Nov 2 '20 at 9:41
  • \$\begingroup\$ I will manually stop it. Just wanted to play around with the code. After that only think what modification I can do to stop it. \$\endgroup\$ – chuackt Nov 2 '20 at 9:43
  • \$\begingroup\$ @chuackt Just make sure to save your project before you run it. When Unity crashes due to running out of memory, then you have to restart and lose any unsaved changes to your project. \$\endgroup\$ – Philipp Nov 2 '20 at 9:44
  • \$\begingroup\$ Thank for your advice, haven't crash it before :) \$\endgroup\$ – chuackt Nov 2 '20 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.