Question by
neuhofflucien · Aug 28, 2020 at 09:28 PM ·
c#instantiatecollidertriggerdestroy object
Destroy instatiate object on trigger enter / collision,destroy instantiate prefab on trigger enter
So, I'm making a game, the goal is to catch fruit that fall randomly of the sky, but I want to when the fruit enter the red bar trigger, it get destroyed, probleme is when the fruit land on the red bar, it get destroyed like 2seconds after, but it doesn't send any message (i add a print to try). I try with tags or not, collisio, or collider. no one worked. sry for bad english
This is the destroy code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class destroyFruitNeg : MonoBehaviour
{
void OnTriggerEnter(Collider fruit)
{
Destroy(fruit.gameObject);
}
}
this is the random spawn fruit code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class deployFruit : MonoBehaviour
{
public GameObject fruitPrefab;
public float respawnTime = 1.5f;
private Vector2 screenBounds;
void Start()
{
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Camera.main.transform.position.x));
StartCoroutine(fruitWave());
}
private void spawnEnemy()
{
GameObject a = Instantiate(fruitPrefab) as GameObject;
a.transform.position = new Vector2(Random.Range(-screenBounds.x, screenBounds.x), -screenBounds.y);
}
IEnumerator fruitWave()
{
while (true)
{
yield return new WaitForSeconds(respawnTime);
spawnEnemy();
}
}
}
and this is the fruit code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Fruit : MonoBehaviour
{
public float speed = 10.0f;
private Rigidbody2D rb;
private Vector2 screenBounds;
void Start()
{
rb = this.GetComponent<Rigidbody2D>();
rb.velocity = new Vector2(0, -speed);
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.y));
}
void Update()
{
}
}
plz help me :D
Comment