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 Evan Stallings · Jul 17, 2013 at 09:38 PM · gameobjectdestroyship

Having Trouble with Destroy (C#)

Hi folks. I have seen several questions about this here on Answers but none of them have helped me even after trying what was suggested. (And, many of them are in UnityScript... I use C#.)

Here is the video and guide series I am watching (it's really great for beginners, by the way) : http://www.youtube.com/watch?v=rldjLcOTmzY

And here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyScript : MonoBehaviour {
     
     Transform myTransform;
     
     public float minSpeed = 5.0f;
     public float maxSpeed = 10.0f;
     public float currentSpeed;
     
     int x;
     int y;
     int z;
 
     void Start () {
         
         x = Random.Range (-6, 6);
         y = 8;
         z = 0;
         
         myTransform = transform;
         myTransform.position = new Vector3 (x, y, z);
         currentSpeed = Random.Range (minSpeed, maxSpeed);
         
     }
         
     void Update () {
         
         x = Random.Range (-6, 6);
         
         myTransform.Translate (-Vector3.up * currentSpeed * Time.deltaTime); 
         
         if (myTransform.position.y < -10) { 
             
             myTransform.position = new Vector3 (x, y, z);
             currentSpeed = Random.Range (minSpeed, maxSpeed); 
         
         }
     }
 
     void OnTriggerEnter (Collider collider) {
         
         //if the laser hits the enemy, destroy
         
         if (collider.gameObject.CompareTag ("Laser")) {
         
         Destroy (this.gameObject);
         
         }
       }
 }

...And I can not seem to get the Destroy part working... This is for a "Galaga"-like game, and the destroy is for the space ships.

(Basically it simply does not delete the ships when I hit them with the lasers.)

Thanks.

Comment
Add comment · Show 5
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 s_guy · Jul 17, 2013 at 10:28 PM 0
Share

Are you sure OnTriggerEnter is getting called? Use a breakpoint or add a Debug.Log() statement to verify.

If not, make sure the object has a collider component on it with the "trigger" box checked.

avatar image Evan Stallings · Jul 18, 2013 at 01:59 AM 0
Share

Alright, thanks. I will check that out when I get a chance and get back here if it does not fix it.

Thanks.

avatar image MadJohny · Jul 18, 2013 at 11:04 PM 0
Share

what he meant is this: before the Destroy line add this line of code: print ("I hitted the trigger"); if it shows on console, the error is totally weird, if notting appears on the console, it means that the OnTriggerEnter event isn't happenning at all, or maybe you didn't set the tag "Laser"

avatar image Evan Stallings · Jul 18, 2013 at 11:09 PM 0
Share

Yep I know what he meant.. though thanks for clarifying how to debug. I will try that and get back... Thanks.

avatar image Evan Stallings · Jul 18, 2013 at 11:11 PM 0
Share

@$$anonymous$$adJohny Yep, nothing showed... How would that be fixed? I am completely beginning with Unity, sorry for being a noob! Thanks.

1 Reply

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

Answer by RyanZimmerman87 · Jul 18, 2013 at 11:26 PM

Hmm I'm kind of in a hurry to work on my own project but I'll give it a shot.

Not really sure I understand your code though.

So is the enemy a spaceship that flies downwards and if it gets too low it respawns at a random position?

So assuming your enemy is a spaceship or some single object and the EnemyScript is attached to it. Do you have a collider attached to this gameobject as well? I normally use Sphere colliders for this.

Does your laser have a collider attached to it as well? The laser collider must have a rigid body attached to the object and also select the collider "Is Trigger" button in the collider options.

And I am assuming you already set the laser tag to "Laser"?

You shouldn't need Destroy(this.gameObject). You can just go Destroy(gameObject);

So if you did all that stuff correctly it should work.

But I've actually never used tags so I can't verify if your code is correct.

I usually just go something like:

  void OnTriggerEnter (Collider collider) {
  
 //if the laser hits the enemy, destroy
  
 if (collider.name == "Laser") 
 {
  
 Destroy(gameObject);
  
 }
 }


It is likely that one of these details isn't correct yet any one setting being wrong could ruin the whole thing. But I think my steps above should solve it if applied correctly.

Depending on how you coded your lasers you may need to adjust the collider.name to "Laser (Clone)"

Comment
Add comment · Show 17 · 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 Evan Stallings · Jul 18, 2013 at 11:32 PM 0
Share

@RyanZimmerman87 Thanks!! I will make sure all of that is true. I have been troubleshooting for the past hour and gotten closer and closer, but I think your suggestions might just solve the problem.

Thanks for taking your time to help me out, all of you. :)

avatar image Evan Stallings · Jul 18, 2013 at 11:42 PM 0
Share

Eh... This is getting a bit ridiculous - I cannot figure it out! I might just re-code the entire project from scratch... I have been using a tutorial but I might be able to do some of it better myself... Thanks for the help again, and if you have any more ideas, feel free to let me know! :)

avatar image RyanZimmerman87 · Jul 18, 2013 at 11:54 PM 0
Share

Ok I'll just outline the steps one more time real quick.

1) Have Enemy Script on the enemy object. Use a sphere collider on this object.

2) Have sphere collider attached to laser object. Set the laser sphere collider to "Is Trigger". Also attach a rigid body object to the laser object. The rigid body should have "Use Gravity" unchecked and "Is $$anonymous$$inematic" checked.

If the laser is called "Laser" in your heirarchy and does in fact collide with the enemy then it should work.

How are you shooting your laser I think the most likely thing is that your laser is either not named "Laser" or that the rigidbody collider are not set up like I just suggested. Or maybe your laser collider is not actually hitting the enemy collider?

Are they both just single game objects with no child objects? And what method are you using to fire the laser?

avatar image Evan Stallings · Jul 18, 2013 at 11:59 PM 0
Share
  1. Already had it. Though - should this also have trigger checked? Because it does :P

  2. Already had it.

The laser is called "LaserPrefab" but I called it that where you called it "Laser," so it's not the issue.

I am shooting it with the Space (fire) bar. I am hitting the enemy.

Yep - no child objects, I am using the space (fire) bar.

Thanks for the continued help.

avatar image RyanZimmerman87 · Jul 19, 2013 at 12:13 AM 0
Share

No the Enemy Script should not have trigger checked. Only the laser needs trigger checked. Hopefully that's your problem.

Show more comments

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

17 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

Related Questions

Multiple Cars not working 1 Answer

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

How to destroy and trigger on enter. 1 Answer

Is it possible to destroy an object at a specific position? 2 Answers

How To Stop Destroyer From Destroying? 2 Answers


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