- Home /
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.
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.
Alright, thanks. I will check that out when I get a chance and get back here if it does not fix it.
Thanks.
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"
Yep I know what he meant.. though thanks for clarifying how to debug. I will try that and get back... Thanks.
@$$anonymous$$adJohny Yep, nothing showed... How would that be fixed? I am completely beginning with Unity, sorry for being a noob! Thanks.
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)"
@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. :)
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! :)
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?
Already had it. Though - should this also have trigger checked? Because it does :P
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.
No the Enemy Script should not have trigger checked. Only the laser needs trigger checked. Hopefully that's your problem.