Question by
gailingen97 · Jan 09, 2021 at 10:48 PM ·
instantiatedestroyitemspawnerspecific
How do I delete a specific prefab clone ?
My itemspawner is spawning a Cube Prefab (my Item) at a 5 sec intervall. Each Cube has a sphere collider wich is a trigger. In the script i gave each cube a value of 1. (the Item amount) I want to delete one of the objects after a few seconds and add up their value, if the triggers are touching eachother. (if possible i want to destroy the first one that spawned in). The goal is to merge them together in order to save performance so it is only one phisical item but with the value of both if they are close enough to each other.
Itemspawner Script:
public class ItemSpawner : MonoBehaviour {
private float nextSpawnTime;
[SerializeField]
private GameObject itemPrefab;
[SerializeField]
private float spawnDelay = 5;
//public Transform spawnPos;
//public GameObject spawnee;
private void Update()
{
if (ShouldSpawn())
{
Spawn();
}
}
private void Spawn()
{
nextSpawnTime = Time.time + spawnDelay;
Instantiate(itemPrefab, transform.position, transform.rotation);
}
private bool ShouldSpawn()
{
return Time.time >= nextSpawnTime;
}
void OnTriggerEnter(Collider collider)
{
Debug.Log("Object entered trigger");
}
void OnTriggerStay(Collider other)
{
Debug.Log("Object is whithin trigger");
}
void OnTriggerExit(Collider other)
{
Debug.Log("Object Exited the trigger");
}
Item Script:
public class Item : MonoBehaviour {
public static int value = 1;
//public GameObject Item;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter(Collider collider)
{
Debug.Log("Object entered trigger");
//coroutine wait for some seconds before fusion
}
void OnTriggerStay(Collider other)
{
Debug.Log("Object is whithin trigger");
//ifobjTrigger touches another objtrigger delete both objs and create obj on its position w. the sum of theire value (maybe this variant ?)
if (other.tag == "Item")
{
//Destroy(Item);
}
//Item.itemAmount = Item.itemAmount + 1;
}
void OnTriggerExit(Collider other)
{
Debug.Log("Object Exited the trigger");
}
}
See the spawner here: https://www.youtube.com/watch?v=BAJWdHM0RUk&feature=youtu.be
Comment