- Home /
Projectile not destroying game object
I'm making a 2D shooter and trying to make an enemy ship be destroyed when the players shoots a projectile at them. I'm able to get points for shooting at the enemy ship but it does not get destroyed and continues to go down the screen before it reset itself back to the top. Another issue I'm having is the player ship not colliding with the enemy ship and would pass through it instead of getting destroyed.
Script on Projectile
using UnityEngine;
using System.Collections;
public class Projectile : MonoBehaviour {
// The game object which spawned us.
public ShipPlayerController ParentShip;
// used to control how fast the game object moves
public float MoveSpeed = 5.0f;
// Use this for initialization
void Start () {
Destroy (gameObject, 1.5f);
// find the player ship game object by name
GameObject playerShip = GameObject.Find ("PlayerShip");
// Acess the player Ship's script component by type
ParentShip = playerShip.GetComponent<ShipPlayerController> ();
}
// Update is called once per frame
void Update () {
transform.position +=
transform.up * Time.deltaTime * MoveSpeed;
}
// Called by the engine when this object collides with another object
void OnTriggerEnter (Collider other) {
if (other.tag == "EnemyShip")
{
Destroy (gameObject);
//Reward the player with some points
ParentShip.ModScore(1);
// Account for this enemy being hit
ParentShip.ModEnemyHits(1);
}
}
}
Script on Enemy
using UnityEngine;
using System.Collections;
public class EnemyControllerBasic : MonoBehaviour {
// List of pickup Types to spawn
public GameObject[] PickupTypes;
// The game object which spawned us.
public ShipPlayerController PlayerShipCtrl;
// Used to control how fast the game object moves
public float MoveSpeed = 5.0f;
// Use this for initialization
void Start () {
Destroy (gameObject, 1.0f);
// Find the player ship game object by name
GameObject playerShip = GameObject.Find("PlayerShip");
// Access the player ship's component by type
PlayerShipCtrl = playerShip.GetComponent<ShipPlayerController> ();
}
// Update is called once per frame
void Update () {
transform.position +=
transform.up * Time.deltaTime * MoveSpeed;
}
// Called by the engine when this object collides with another
void onTriggerEnter (Collider other) {
// Hit the player's projectile?
if (other.tag == "PlayerProjectile")
{
// A projectile instant kills us
Destroy(gameObject);
// Drop a pickup
SpawnPickup();
}
// Hit the player's ship
else if(other.tag == "PlayerShip")
{
// Wrecking with the player kills us
Destroy (gameObject);
// Remove points from the player for hitting enemy
PlayerShipCtrl.ModScore(-1);
}
}
// Creates a random Pickup at the game object's current location
void SpawnPickup ()
{
// randmoly choose an index in the pickup array
int randIndex = Random.Range (0, PickupTypes.Length);
// Use the random index to select one of the enemy tpyes to spawn
Instantiate (PickupTypes [randIndex],
transform.position, transform.rotation);
}
}
Are you using 2D colliders or 3D colliders, and is Trigger checked in the inspector? Also, the "o" in your onTriggerEnter should be capitalized. When in doubt, a basic test is to put Debug.Log() in a method you believe you should be entering.
Answer by bilo-lwabona · Sep 14, 2014 at 09:39 PM
On Trigger enter only works if the isTrigger is checked on the collider in the inspector. Note that with this property enabled, there won't be a phsical collision. Not saying that your approach won't work, but OnCollisionEnter is an alternative (and works in pretty much the same way, except that objects shouldn't be able to phase through each other).
Your answer
![](https://koobas.hobune.stream/wayback/20220613163007im_/https://answers.unity.com/themes/thub/images/avi.jpg)