Creating an Auto-Restart
I have a game where you roll a ball around and collect a bunch of yellow cubes. I have a restart button already, so the player can click it when they mess up or want to restart, but I want there to be an auto-restart/teleport/portal so that if the player falls to the lower ground, they get sent back to the starting position, and their score resets. Here is a small picture.
All answers are appreciated! Thanks in advance!
Answer by Alokdo · Nov 04, 2016 at 02:48 AM
Try putting a cube where when the player touch the cube, the game will restart.
Remove the "Mesh renderer" (or not, but the cube will be able to see) and, in the BoxCollider check the parameter "is trigger"
Now, make a script (JavaScript or UnityScript) with the following:
#pragma strict
function OnTriggerEnter (other: Collider){
//Here is where you need to put your restart code, the same code as the button
}
Attach the script to the cube and try it.
Tell me if it worked. Greets
$$anonymous$$y restart button was a C sharp script, so will the same code work on Java?
Ok, use this code ins$$anonymous$$d:
using UnityEngine;
using System.Collections;
public class SCRIPTNA$$anonymous$$E : $$anonymous$$onoBehaviour {
void OnTriggerEnter(Collider other) {
//Here is wher you need to put your restart code, the same code as the button
}
}
Where it says "SCRIPTNA$$anonymous$$E" put the name of your script.
Says there are compiler errors with the script.
Here is my RESTART BUTTON script:
Here is the one I am trying to create now, the AUTO-RESTART script:
Tell me if I should make any changes.
Please, if you can, write the code in the commentary, because the images are broken.
RESTART:
using UnityEngine; using UnityEngine.Scene$$anonymous$$anagement; using System.Collections;
public class Restart : $$anonymous$$onoBehaviour {
public void RestartGame() { Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().name); // loads current scene
}
AUTO-RESTART:
using UnityEngine;
using UnityEngine.Scene$$anonymous$$anagement;
using System.Collections;
public class Background : $$anonymous$$onoBehaviour {
void OnTriggerEvent (Collider other) {
public void RestartGame()
{
Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().name); // loads current scene
}
}
}
(I know this is confusing but I didn't know how else to put it without a picture.)
The name of this script will be "Restart" (without quotes)
Ok, this needs to work (i tested it):
using UnityEngine;
using System.Collections;
using UnityEngine.Scene$$anonymous$$anagement;
public class Restart : $$anonymous$$onoBehaviour {
void OnTriggerEnter(Collider other) {
Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().name);
}
}
Answer by PizzaPie · Nov 08, 2016 at 02:12 PM
Well or you can have a simple statement like this
float someValue;
void Update()
{
if(player.transform.position.y <someValue)
RestartGame();
}
set the someValue to a point below your plane and it should work. That way you don't have to place triggers all around the plane but it have some restrictions on the way you design the level. (if you attach that code to the player change the statement to gameObject.transform.position.y)