I made my first minigame where you shoot to objects and if there are more then 10 objects on the screen you lose. I wanted do let player press R after lose to restart a level but it’s not happaning. I see that level is reloading but everything is the same. There is still “Gameover text” display, there are more spawned objects then before and so on. I’ve tried with enter code here Application.LoadLevel("Minigra");and with SceneManager.LoadScene ("OtherSceneName", LoadSceneMode.Additive);. And it’s all the same. Can you help me? There is my code:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class Controller : MonoBehaviour {
public GUIText scoreText;
public int score;
public GUIText restartText;
public GUIText gameOverText;
public GUIText countTargetText;
public GameObject target;
private float nextDrop = 0f;
private float dropInterval = 2f;
private float changeInterval = 5f;
private bool gameOver;
GameObject[] countTarget;
void Start()
{
score = 0;
UpdateScore();
gameOver = false;
UpdateScore();
restartText.text = "";
gameOverText.text = "";
countTargetText.text = "0/10";
InvokeRepeating("Target", 1, nextDrop);
}
void Target()
{
float x = Random.Range(-26f, 26f);
float z = Random.Range(-22f, 27f);
Instantiate(target, new Vector3(x, 0, z), Quaternion.identity);
}
void Update()
{
if (Time.time >= nextDrop) //If ready to spawn
{
Target();
nextDrop += dropInterval; //Set next spawn time
if (Time.time >= changeInterval) //If ready to change spawn interval
{
if (dropInterval > 0.5f) //Change spawn interval to 3/4ths what it was
dropInterval *= 0.80f;
else //Make sure dropInterval stays above 1.
dropInterval = 0.5f;
}
}
countTarget = GameObject.FindGameObjectsWithTag("Target");
countTargetText.text = countTarget.Length + "/10";
if (countTarget.Length >= 10)
{
gameOverText.text = "Game Over!";
gameOver = true;
}
if (gameOver)
{
restartText.text = "Naciśnij R żeby zrestartować";
if (Input.GetKeyDown(KeyCode.R))
{
SceneManager.LoadScene("Minigra");
Destroy(GameObject.FindWithTag("Target"));
gameOver = false;
}
}
}
public void AddScore(int newScoreValue)
{
score += newScoreValue;
UpdateScore();
}
void UpdateScore()
{
scoreText.text = "Punkty: " + score;
}
}