After i press restart button game restarts but it does not spawn the targets when i first start the game it works but after restarting it does not spawn targets, cursor should also disappear and turn it crossheir. it tried many things but it does not work
public int totalHits = 0;
public int ballHit = 0;
public Image accuracyImage;
public TextMeshProUGUI countDownText;
public TextMeshProUGUI scoreText;
public TextMeshProUGUI performanceText;
public TextMeshProUGUI ballHitText;
public TextMeshProUGUI percentageText;
public float countDown;
public float accuracy = 0f;
public float accuracyFA = 0f;
public bool isGameOver = false;
public FirstPersonController fpsScript;
void Awake()
{
isGameOver = false;
countDown = 120;
StartCoroutine(Spawner());
//InvokeRepeating("Spawner", 1, 1);
}
// Update is called once per frame
void Update()
{
accuracy = ballHit * 100 / totalHits;
if (countDown > 0)
countDown -= Time.deltaTime;
countDownText.text = (countDown).ToString("0");
scoreText.text = accuracy + "%";
if (countDown < 1)
{
isGameOver = true;
}
if (isGameOver)
{
GameOver();
}
accuracyFA = ballHit / totalHits;
//Spawner();
}
IEnumerator Spawner()
{
while (true)
{
yield return new WaitForSeconds(0.5f);
if (countDown > 0 && noOfBalls <= 3)
{
Vector3 position = new Vector3(Random.Range(-270, 230), Random.Range(20, 180), 355);
Instantiate(target, position, target.transform.rotation);
Debug.Log("Spawned");
noOfBalls++;
}
}
}
void GameOver()
{
Time.timeScale = 0f;
fpsScript.RotationSpeed = 0f;
gameOverMenu.SetActive(true);
accuracyImage.fillAmount = accuracyFA / 10;
ballHitText.text = (ballHit).ToString("0");
percentageText.text = accuracy + "%";
HUD.SetActive(false);
if(ballHit <= 40)
{
performanceText.text = "Poor, Need Practice";
}
else if(ballHit <= 70)
{
performanceText.text = "Good, Can Improve";
}
else if(ballHit >= 70)
{
performanceText.text = "Well Done, Keep It Up";
}
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
crossheir.SetActive(false);
}
public void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
targets should spawn and cursor should disappear and turn into crossheir
I am editing as after a conversation with ipodtouch I found my first part was incorrect since yield would cause the While(true)
loop to exit.
What ipodtouch and bugfinder pointed out is a good point on what might be causing the problem. In the Gameover
method you set the Time.timescale
to 0 which does not restart when loading the scene again. It may be prudent to return it to 1 (Or whatever value you are using in-game, normally it is 1) at the end of the method or when starting the scene. It might be best to not even change Time.timescale
as it can mess up code that might be running.
While I also understand and agree with iPodtouch that noOfballs would restart after loading the scene again. The variable doesn't appear in the provided code and it's important to establish it in the script to know its starting values where it is coming from.
private int noOfBalls = 0;
void Awake()
{
noOfBalls = 0;
isGameOver = false;
countDown = 120;
StartCoroutine(Spawner());
//InvokeRepeating("Spawner", 1, 1);
}
The reason why the crosshair is likely not returning is because as previously mentioned Time.timescale
is set to 0 and the script in charge of making said change is paused. It is also likely that the script in charge of changing the mouse to the crosshair is in another scene so it isn't been run again. Make sure that it is being called.