I currently have this code.
using UnityEngine;
public class gameover : MonoBehaviour {
void OnTriggerEnter(Collider thisObject) {
if (thisObject.tag == "Player" ){
Application.LoadLevel("Intro");
}
}
}
I have added this code to a projectile that is launched towards the player. Currently it just hits the player, does nothing and keeps going. I cannot tick the projectile as the trigger because if I do a 2nd script I have on it will delete everything it touches? I’m hoping someone can help me with this.
In answer to stingman’s comment :
This is my wall destroy code. At the moment it now just goes right through the wall. I have ticked is trigger for the wall that is meant to be destroyed.
using UnityEngine;
public class DestroyWall : MonoBehaviour {
void OnTriggerEnter(Collider thisObject) {
if (thisObject.tag == "destructableWall" ){
Destroy (thisObject);
}
}
}
also with that other script am i meant to add it to my player or projectile and then tick the opposite.
If the wall is a trigger, the projectile must be a rigidbody - is it? And you should use Destroy(thisObject.gameObject), since Destroy requires a GameObject reference (and thisObject is a Collider). But you would have problems with the gameover code, because making the player a trigger definitely isn’t a good idea.
Anyway, that’s not the best way to do what you want: ideally, the projectile should be a rigidbody and the wall a regular collider (not trigger). You should also use OnCollisionEnter in both cases (assumed both scripts attached to the projectile):
using UnityEngine;
public class gameover : MonoBehaviour {
void OnCollisionEnter(Collision coll) {
if (coll.gameObject.tag == "Player" ){
Application.LoadLevel("Intro");
}
}
}
// and in the DestroyWall script:
using UnityEngine;
public class DestroyWall : MonoBehaviour {
void OnCollisionEnter(Collision coll) {
if (coll.gameObject.tag == "destructableWall" ){
Destroy (coll.gameObject);
}
}
}
Notice that both scripts could be combined in a single one, improving the performance:
using UnityEngine;
public class ProjectileScript : MonoBehaviour {
void OnCollisionEnter(Collision coll) {
String hitTag = coll.gameObject.tag;
if (hitTag == "destructableWall" ){
Destroy (coll.gameObject);
}
else
if (hitTag == "Player"){
Application.LoadLevel("Intro");
}
}
}