Restart when collision?

Hey Guys
I want to make that if my player collides with a cube the level restarts…
i only have this script but thats not excactly what i want…

function Update ()

{
 
    if(Input.GetKeyDown("r")) Application.LoadLevel(0);

}
function OnControllerColliderHit(other: ControllerColliderHit) // if you use a rigidbody instead, use OnCollisionEnter(other : Collision)
     if(other.gameObject.tag == "Enemy")
          Application.LoadLevel(Application.loadedLevel);
}

i changed it with the other one, but it just gives errors like that function is unexpected…
where exactly do i need to put it in?

like that:

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {


		function OnCollisionEnter(other: Collision) // if you use a rigidbody instead, use OnCollisionEnter(other : Collision)
			
			if(other.gameObject.tag == "Player")
				
				Application.LoadLevel(Application.loadedLevel);
	
}

You need opening and closing braces for the function.

Erm, i thought you wanted to have javascript code as you posted your sample code in javascript aswell… You just added the JavaScript code i posted into a C# class, which will never ever work. :stuck_out_tongue:

Make sure the code i posted is placed into a new JavaScript file.

In case you want to stick with C# though, you can remove the code you added and put the following instead:

void OnCollisionEnter(Collision other)
{
     if(other.gameObject.tag == "Player")
          Application.LoadLevel(Application.loadedLevel);
}

In your example, this should look like

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour 
{
     void OnCollisionEnter(Collision other)
     {
          if(other.gameObject.tag == "Player")
               Application.LoadLevel(Application.loadedLevel);
     }
}

However, you will probably need a movement via script for the character controller as moving it via editor during runtime will not cause a collision if i’m not mistaken. Not quite sure though, just ask if you still need some help.

Also, when using C# and you inherit from MonoBehaviour, your class always needs to have the same name as the .cs file it is placed in, otherwise you will not be able to attach it to any object, but Unity will tell you in case you forget about that.

First of all thanks for helping me, i really have no idea how to script:o
So i added this script to my cube but if i run into it nothing happens?
I use a movement script which is not finished jet, because my charcter can fly when he jumps more than once… :frowning:
Do i need to put the script into the movement script? Or do i need to change some settings on the cube?
Thanks for helping me
Here is my movementscript:

public class PlayerMovement : MonoBehaviour {
	
	private bool grounded;
	
	private float speed;
	
	private Vector2 startPos;
	
	//
	void Start () {
		
		speed = 7;
		
		grounded = true;
		
	}
	
	
	//
	void FixedUpdate ()
	{
		
		transform.Translate(-Vector3.forward * -speed * Time.deltaTime);//
		
		#if UNITY_EDITOR
		
		transform.Translate (Vector2.right * Input.GetAxis ("Horizontal") * 5 * Time.deltaTime);
		if (Input.GetKey (KeyCode.Space)  grounded == true)
		{
			rigidbody.AddForce (0,50,0,ForceMode.Force);
			grounded = true;
			
		}
		#endif
		#if UNITY_ANDROID
		transform.Translate (Vector3.right * Input.acceleration.x * 7.5f * Time.deltaTime);
		if (Input.touchCount > 0)
		{
			
			Touch touch = Input.touches [0];
			
			
			switch (touch.phase) 
				
			{	
				
			case TouchPhase.Began:
				startPos = touch.position;
				break;
				
				
			case TouchPhase.Moved:
				if (Mathf.Abs (touch.position.y - startPos.y) > 100) //
					
				{
					
					float swipeValue = Mathf.Sign (touch.position.y - startPos.y);
					
					if (swipeValue > 0  grounded == true) //
					{
						rigidbody.AddForce (0,50,0,ForceMode.Force);
						grounded = true;
						break;
					} 
					else if (swipeValue < 0) //
					{	 //
						break;
					}
				}
				break;
			}
		}
		#endif
	}
}

For jumping, you might want to use Input.GetKeyDown instead, since the GetKey function will fire as long as the key is pressed (function will be fired every frame the key is still down => several calls if you’re not watching a slow diashow)

In order to not fly away or add any jumping force while jumping, your variable ‘grounded’ has to be set to false after you started to jump, then find a way to check if you’Re still jumping or back on the ground and set it back to true again.

The code i provided can either be taken over as a complete new script and be attached to the object you want to use it (the object that shall collide with cubes) on or you implement the function (without the class definition) into your current script.

If you run into the object and still nothing occurs, check the tag of the object you are trying to collide with, make sure it’s collider is activated and not set as trigger. Also you can always insert a Debug.Log(variable or text in " ").

Im using a new script now so the problem with jump is no more a problem :wink:
Also i have a script which makes that if he falls -0 the game restarts… but when i put your script on the cube which my player shouldnt run into it or it restarts nothing happens?? I have a collider and no trigger like u told me…
I really dont get it :frowning:
And what do you mean with the Debug.log?

For example you can use :

Debug.Log("The game should restart");

where you think that there might be the error that won’t let the game restart to see if the debug.Log is called or not, at least. If there’s a collision and that your debuglog is not called it means that your way to check collision is not good (if you have placed the line of code at the good point. But don’t forget to remove your debuglogs from the script before you build the game because they will kill your frame rate.

I added this to my script and now it works… but if i put it away it dont work no more???

If it works with the debuglog it should work without.

Ok now i get it… it was because the debuglog have this ;
haha…
Suddoha didnt wrote this when he gave me the script:smile:
after this line if(other.gameObject.tag == “Player”) it needs a ;