Destroy specific object with tag
I have this in my script:
void OnTriggerEnter(Collider other) { if(other.tag == "Dead") { }
I want to destroy that object im touching with the tag "Dead". But not destroy all Object with tag Dead. Something like: Destroy(Other.tag = "Dead");
Answer by bubzy · Oct 04, 2015 at 09:37 PM
that should only destroy the object that you collide with.
Destroy(other.gameObject);
should work for you
Answer by $$anonymous$$ · Oct 06, 2015 at 08:36 AM
I'm having similar problems. I got health on target. But I hit the target and the bullets bounce off. in order to get the bullet clones to go away I already added a destroy script to the bullet. If i try and change this, it stops working. But then again aside from stopping being able to actually "Shoot" and self destruct, any changes I made to the code causes it to stop working all together and gives me a bunch of errors.
public class MoveBullet : MonoBehaviour {
public float speed = 1.0f;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Translate(0, 0, speed);
Destroy(gameObject, 6); //<===This only works to make the bullet go away ===
}
}
Enemy health is as follows and while there are no "errors" nothing actually happens to the enemy target.
public class Health : MonoBehaviour {
public float health = 100f;
public void RemoveHealth(float amount)
{
health = health - amount;
if (health <= 0) { Destroy(gameObject); }
}
void Start()
{
}
}
The shooting script is as follows and each time I try and modify it any further, it stops working.
public class Shoot : MonoBehaviour { public GameObject bullet; public float delayTime = 1;
private float counter = 0;
void Start()
{
}
void Update()
{
if (Input.GetKey(KeyCode.Mouse0) && counter > delayTime)
{
Instantiate(bullet, transform.position, transform.rotation);
GetComponent<AudioSource>().Play();
counter = 0;
}
counter += Time.deltaTime;
}
}
Enemy Following Script as Enemy_AI works but the same problem occurs where if i try and modify it, it stops working. On top of that, NO Enemy Spawn script will even add without a bunch of errors.
public class Enemy_AI : MonoBehaviour {
Transform tr_Player;
float f_RotSpeed = 3.0f, f_MoveSpeed = 3.0f;
// Use this for initialization
void Start () {
tr_Player = GameObject.FindGameObjectWithTag("Player").transform;
}
// Update is called once per frame
void Update () {
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(tr_Player.position - transform.position), f_RotSpeed * Time.deltaTime);
transform.position += transform.forward * f_MoveSpeed * Time.deltaTime;
}
}
No solutions seem to be forthcoming especially any tutorials showing the usage of the Visual Studio or anything with current instructions that are not constantly failing codes. It's really irritating that after spending 4 weeks just to get to this point to not be able to spawn enemies, or destroy them and i haven't even bothered trying to work on getting enemies to shoot back or take damage.
It makes things really frustrating to say the least all the references I get sent to is incompatible with the current engine, which is 5.2 and nothing of any use by way of tutorial videos for 2015, and now needed for 2016 in just a couple of months. If you have solved any of these things "PLEASE" help!!!
Understand I am very new to coding and only tried to do stuff a few years back before giving up. The fact I even managed to get this far with static objects (not having any success with humanoid avatars I made with "MakeHuman" which always get's messed up upon Import and no instructions are of any help).
PS. All targets i want to destroy are tagged "Enemy." I really doubt what was being asked was to simply "destroy any object" in a scene, like for example, the terrain. Just the intended targets.