Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 KingSloth · Jun 20, 2017 at 08:41 PM · c#collisioncollision detectionontriggerenterbullet

Could some one help me with this script...

Hello. I really don't know what I am doing, so if some one could help me figure this script out, that would be great. I am trying to get the bullet to destroy the Enemy.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SceneController : MonoBehaviour {
 
     public GameObject Enemy;
     public GameObject Bullet;
 
     void OnTriggerEnter(Collider Bullet) {
         Destroy(Enemy);
     }
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         OnTriggerEnter ();
     }
 }

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 sleepandpancakes · Jun 20, 2017 at 10:31 PM 0
Share

please make sure your code is formatted well so we can help you more easily

5 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by altaiirdesmond · Jun 21, 2017 at 04:56 AM

@KingSloth 1. Create new script "DestroyEnemy" 2. Add new tag "Bullet" 3. in your bullet gameObject or prefab assign it to "Bullet" tag. 4. Finally put this script to your Enemy gameObject or prefab

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class DestroyEnemy: MonoBehaviour {
  
      void OnTriggerEnter(Collider other) {
          if(other.gameobject.tag == "Bullet")
                     Destroy(gameObject);
      }
  }



Comment
Add comment · Show 1 · 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
avatar image KingSloth · Jun 21, 2017 at 06:01 PM 0
Share

Thank you so much!

avatar image
2

Answer by JxWolfe · Jun 20, 2017 at 09:59 PM

in void Update you are trying to call OnTriggerEnter. OnTriggerEnter is called when your collider's trigger hits something

Comment
Add comment · Show 3 · 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
avatar image KingSloth · Jun 20, 2017 at 10:01 PM 0
Share

I tried taking it out but it didn't work.

avatar image JxWolfe KingSloth · Jun 20, 2017 at 10:07 PM 0
Share

After you take it out you have to add a collider to your object with that script. Then there is a button called isTrigger, you need to set it to true.

avatar image eskivor KingSloth · Jun 20, 2017 at 10:44 PM -1
Share

then you have to add a rigidbody (or a rigidbody 2d if you use 2d colliders) one at least one of the two objects you are trying to check the collision (the bulllet, the enemy or both), and be sure that your two objects are on layers that can collide between them (use Edit -> Projects Settings -> Physics (or Physics 2D) to check that.

avatar image
2

Answer by cstooch · Jun 20, 2017 at 11:49 PM

There is some good help here, but really, you should do some tutorials, as you seem a little lost on what to do - which is perfectly normal.. all of us were there once. There are a lot of great ones here: https://unity3d.com/learn/tutorials

This one here has a tonne of shooter concepts in it that you could use: https://unity3d.com/learn/tutorials/projects/survival-shooter-tutorial

This is a space-ship shooter, but even this would help you understand more: https://unity3d.com/learn/tutorials/projects/space-shooter-tutorial

Comment
Add comment · Show 1 · 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
avatar image KingSloth · Jun 21, 2017 at 06:00 PM 0
Share

Thanks! I will check out that tutorial!

avatar image
-1

Answer by Bren0831 · Jun 21, 2017 at 01:53 AM

You should just put this script on an enemy. I think that should work.

Comment
Add comment · Show 1 · 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
avatar image KingSloth · Jun 21, 2017 at 06:00 PM 0
Share

Thanks for helping!

avatar image
0

Answer by OGSynapse · Jun 20, 2017 at 11:10 PM

What I think is the problem is that you are calling OnTriggerEnter in this line - void OnTriggerEnter(Collider Bullet) { Destroy(Enemy); }what I think you could do is change OnTriggerEnter to OnCollisionEnter or OnCollisionStay which would probably fix it. Hopefully I helped!

Comment
Add comment · Show 1 · 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
avatar image KingSloth · Jun 21, 2017 at 06:00 PM 0
Share

Thanks for your help!

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

349 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Destroying object on collision 3 Answers

Collision detection from specific directions using rigid body character? 0 Answers

How to detect collision on only one side of an object? [C#] 4 Answers

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

How to make gameObject make only one collision 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