- Home /
This question was
closed Nov 09, 2017 at 08:01 AM by
Mcbainey for the following reason:
The question is answered, right answer was accepted
Question by
Mcbainey · Nov 08, 2017 at 12:04 PM ·
prefaboverlappingdestroy-clones
Script won't destroy prefab clones when overlapping... plz help
using UnityEngine; using System.Collections;
public class TruckSpawner : MonoBehaviour {
public float spawnDelay = .3f;
public GameObject IncommingObject;
public Transform[] spawnPoints;
float nextTimeToSpawn = 0f;
GameObject ThePrefabClone;
void Update()
{
if (nextTimeToSpawn <= Time.time)
{
SpawnCar();
nextTimeToSpawn = Time.time + spawnDelay;
}
}
void SpawnCar()
{
int randomIndex = Random.Range(0, spawnPoints.Length);
Transform spawnPoint = spawnPoints[randomIndex];
ThePrefabClone = Instantiate(IncommingObject, spawnPoint.position, spawnPoint.rotation) as GameObject;
Destroy(ThePrefabClone, 7.0f); //spawns last for 7 seconds
}
void OnCollisionEnter2D(Collision2D coll)
{ if (coll.gameObject.tag == "PassingObject") { Destroy(coll.gameObject); } }
}
Comment
Did you make sure that the tag is the same as the one on the object (tags are case sensitive)? Also, make sure that both objects have a collider not set to Is Trigger, and one of 2 objects colliding must have a RigidBody2D component.
Answer by Mcbainey · Nov 08, 2017 at 01:28 PM
it gives me no errors... like its skipping and doing nothing.