Can't restart a level
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;
}
}
Answer by Zynek · Nov 05, 2016 at 09:30 AM
You could try
SceneManager.LoadScene ("OtherSceneName", LoadSceneMode.Single);
Also i would advise to reorganize as the code after "LoadScene" should not execute.
Destroy(GameObject.FindWithTag("Target"));
gameOver = false;
SceneManager.LoadScene ("OtherSceneName", LoadSceneMode.Single);
Keep in mind that objects tagged with DontDestroyOnLoad(Object) wont get destroyed https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
I tried Scene$$anonymous$$anager.LoadScene ("OtherSceneName", LoadScene$$anonymous$$ode.Single);
and t's still the same. Before I press R my scena looks like this: http://oi63.tinypic.com/20jo57d.jpg And after http://oi64.tinypic.com/fc91n7.jpg
And my code goes like this:
using UnityEngine;
using System.Collections;
using UnityEngine.Scene$$anonymous$$anagement;
public class Controller : $$anonymous$$onoBehaviour {
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 //$$anonymous$$ake 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.Get$$anonymous$$eyDown($$anonymous$$eyCode.R))
{
Scene$$anonymous$$anager.LoadScene("$$anonymous$$inigra", LoadScene$$anonymous$$ode.Single);
}
}
}
public void AddScore(int newScoreValue)
{
score += newScoreValue;
UpdateScore();
}
void UpdateScore()
{
scoreText.text = "Punkty: " + score;
}
}
Any idea how to fix this? I stuck with this problem for several hours now I I'm out of idea.
I spend two days trying diffrent things and still nothing works. It's very strange. It look like after reloding only player position is restarted nothing else. I also don't understand why CancelInvoke(); does not work. Also after restarting displayed colors are difrent as you can see on screen shots Before and After. This is small game so I even tried to make new project and make it again (with the same code) and still nothing. I'm really desperate, I'm afraid to start any bigger procjets without solving this problem.
Answer by Golauszko · Nov 05, 2016 at 01:08 PM
I think somethng serious is wrong with this code but I still don't know what. I add ' if (countTarget.Length >= 10) { gameOverText.text = "Game Over!"; gameOver = true; CancelInvoke(); '
And text "Game over" is displayed properly but Invoike is still going. Also whan I want to restar it's not working.
Any clue what I'm doing wrong?
I honestly don´t, have you tried to play around with Scene$$anonymous$$anager ? Like UnloadScene for example, then load it again ?
https://docs.unity3d.com/ScriptReference/Scene$$anonymous$$anagement.Scene$$anonymous$$anager.html
Your answer
![](https://koobas.hobune.stream/wayback/20220612100101im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Main Menu Help 2 Answers
Rerunning a script on a game object 1 Answer
Restart current level after deathscreen 3 Answers
Level Unlock script help 1 Answer