Good morning, So my class group has been tasked to make a game and we have been stuck on how to solve our issue with a script that instantiates a prefab after deleting the first prefab. What happens is sometimes the prefab will be duplicated several times over as shown in the link provided.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
* ATTACH THIS SCRIPT TO THE OBJECT THAT IS BEING DESTROYED
*/
public class breakObject : MonoBehaviour
{
// Spawns destroyed prefab
public Transform destroyedObject;
// Strength of object
public float objectStrength;
// The distance that the force effects the object
public float objectExplosionRadius;
// The distance that the "explosion" sends the pieces
public float explosionPower;
// The force that sends the broken pieces flying up
public float upwardsExplosion;
// When object hits the prefab hard enough destroys the object.
private void OnCollisionEnter(Collision collision)
{
// Anything that hits the prefab with greater force than the strength of the object is destroyed.
if (collision.relativeVelocity.magnitude > objectStrength)
{
Destroy(gameObject);
Instantiate(destroyedObject, transform.position, transform.rotation);
destroyedObject.localScale = transform.localScale;
Vector3 explosionPos = transform.position;
Collider[] colliders = Physics.OverlapSphere (explosionPos, objectExplosionRadius);
foreach (Collider hit in colliders)
{
if (hit.attachedRigidbody)
{
hit.attachedRigidbody.AddExplosionForce(explosionPower * collision.relativeVelocity.magnitude, explosionPos, objectExplosionRadius, upwardsExplosion);
}
}
}
}
}
This script is attached to the parent object of the prefab. We have tried commenting out several lines to see if it would change anything, but now its believed to be something with unity’s physics. The Prefab does have a rigid body attached and a box collider.
