Problem is not reproducible or outdated
Why does my script and collider2D automatically deactivate after reloading the scene?
Hi, I'm making a game where when the screen is tapped, the player sprite impacts the pig sprite. After the first collision, the pig sprite spawns randomly in a given area. The game works as intended when I press the play button inside of Unity. However, when I reload the scene via script attached to a UI button, the collider2D and script attached to the pig sprite automatically deactivates. In the middle of the game if I manually reactivate the collider2D and script for the pig sprite, the game works fine again.
The player:
The Pig:
The collision method for the pig sprite:
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "player")
{
Instantiate(col.gameObject);
Destroy(col.gameObject);
Instantiate(pig, RandomPositon(), Quaternion.identity);
}
}
The collision method for the player sprite:
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "pig")
{
Destroy(col.gameObject);
score.IncrementScore();
}
}
The method attached to the UI button responsible for reloading the scene:
public void RestartGame()
{
SceneManager.LoadScene(0);
}
Follow this Question
Related Questions
Need Help with a Level Reloading Script (c#) 1 Answer
Switch scene on collsion C# 1 Answer
If Statement not working properly - Comparing game objects 0 Answers
How can i restart a script on collision ? 1 Answer
ScreenToWorldPoint on One Axis? 0 Answers