How can I destroy my Player when it touches another object?

I am making a 2D game and I need my player to die when it touches another object. How can I do this?!?!

Give the other object one of the 2d colliders, set it as trigger. Make a script so that when the player enters the collider, the object destroys the player by using Destroy(hit.collider.gameObject); Something like that

1 Answer

1

using UnityEngine;
using System.Collections;

public class SandCubeistrigger : MonoBehaviour {

public static Vector3 touched_object;
public static bool Colexists=false;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

	
}


	
	void OnTriggerEnter(Collider other) {
	
	if (other.gameObject.tag == "theobjecttagnameuwannatouch")
	{
		
		Colexists=true;
		
		Destroy(this.gameObject);
		
	}
}


  void OnTriggerExit(Collider other) {
	
  Colexists=false;
		//Debug.Log("Collission does not exist");

	
}

 void OnTriggerStay(Collider other) {
	if (other.gameObject.tag == "sandcube")
	{
	 Colexists=true;
		
	}
 }

i didn't think about this but it makes sense. Should I add material as global variable and then use renderer.material in function Start?

That would work. var myMaterial : Material; function Start() { renderer.material = myMaterial; } Aww my +1 disappeared when I converted to answer ><

I edited the script and now I see it is a problem with array, question edited.

Are the objects pre-instantiated? If you mean to be creating objects, you'll probably need to Instantiate them.

http://docs.unity3d.com/ScriptReference/ObjectInstantiate.html - did you mean this Instantiate? it looks like something for making clones and my electrodes will have very weird positions so it doesn't make sense to me while I wrote already lines for them separately. Maybe I don't understand. The error is in array.