- Home /
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 ();
}
}
please make sure your code is formatted well so we can help you more easily
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);
}
}
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
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.
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.
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
Answer by Bren0831 · Jun 21, 2017 at 01:53 AM
You should just put this script on an enemy. I think that should work.
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!