- Home /
Prefab Duplication issue
The prefab is a hazard, and when the ball collides with the trigger, it triggers the game over panel.
However, only the last prefab dragged into the hierarchy does this correctly, not all of them.
The prefab has the 'GameOver' script attatched:
public void OnTriggerEnter(Collider collider)
{
if (collider.tag == "Player")
{
//gameOver
gameOver = true;
//disable mesh
collider.gameObject.GetComponent<MeshRenderer>().enabled = false;
//freeze pos
PlayerController.instance.playerRb.constraints = RigidbodyConstraints.FreezeAll;
//disable move buttons
foreach (GameObject buttons in MoveButtons)
{
buttons.SetActive(false);
}
//instantiate explosion
Instantiate(playerExplosion, collider.transform.position, Quaternion.identity);
}
}
}
And the GameManager has the following:
public void Update ()
{
isGameOver = GameOver.instance.gameOver;
//check for game over
if (isGameOver)
{
gameOverPanel.SetActive(true);
}
}
Why does this only work for the last placed prefab ? any thoughts ?
Here is a video of the problem - https://youtu.be/QXo2AtA9dQg
Thank you.
Answer by Casiell · Oct 11, 2018 at 09:58 AM
Ok, it seems like your GameOver script has a static instance. You cannot have a static instance of a script that is on multiple objects! If that's the case then each next object with this component overwrite the instance with a reference to itself, that's why only the last one works
Make your gameOver field 'public static' and reference it instead.
public void Update ()
{
isGameOver = GameOver.gameOver;
//check for game over
if (isGameOver)
{
gameOverPanel.SetActive(true);
}
}