Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DanielB89 · Sep 14, 2014 at 08:33 PM · collisiondestroyprojectile

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);
     }
 
 }




Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image supernat · Sep 14, 2014 at 08:50 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Destorying an object when it hits the ground 1 Answer

How to delete this projectile after touching anything?? (C#) 1 Answer

How to only delete one of two collided objects? 1 Answer

Collision between 2 prefabs. 0 Answers

Finding a script on an object 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges