Trying to Re-Instantiate the Zombie(main character) again when replay button is clicked, but unable to

Viewed 25

I am making Replay logic for my game, where when I click replay I got to the Main Page. The problem I am facing is that after clicking Play on the game after coming from Replay, the Zombie character in my game is not showing up. The game is running without the player. I am posting the script, check the Replay function which is attached to Replay button in the game.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;

public class GameManager : MonoBehaviour
{
public static GameManager instance = null;

private bool playerActive = false;
private bool gameOver = false;
private bool gameStarted = false;
private GameObject newZombie;

[SerializeField] private GameObject mainMenu; //contains main menu content
[SerializeField] private GameObject endGame; //contains game over content

[SerializeField] private GameObject zombie;

public bool PlayerActive{
    get{
        return playerActive;
    }
}

public bool GameOver{
    get{
        return gameOver;
    }
}

public bool GameStarted{
    get{
        return gameStarted;
    }
}

void Awake()
{
    if(instance == null){
        instance = this;
    }else if(instance != this){
        Destroy(gameObject);
    }

    Assert.IsNotNull(mainMenu);
    Assert.IsNotNull(endGame);

    DontDestroyOnLoad(gameObject);
}

// Start is called before the first frame update
void Start()
{
    endGame.SetActive(false);
    mainMenu.SetActive(true);      
}

// Update is called once per frame
void Update()
{
   
}

public void PlayerCollided()
{
    gameOver = true;
    endGame.SetActive(true);
    mainMenu.SetActive(false);
    DontDestroyOnLoad(gameObject);
   

}

public void PlayerStartedGame()
{
    playerActive = true;
}

public void EnterGame()
{
    endGame.SetActive(false);
    mainMenu.SetActive(false);
    gameStarted = true;
}

public void Replay()
{
   
    endGame.SetActive(false);
    mainMenu.SetActive(true);
    gameOver = false;
    newZombie = Instantiate(zombie) as GameObject;
   
}
1 Answers

There are a lot of assumptions we have to make based on the information you gave.

Try instantiating the zombie on a specific location. You're using Instantiate(gameObject), but there's a different variant to the Instantiate method which also takes a Vector3 position to spawn the object as a second argument.

If that doesn't work, please reply with answers to the following questions:

  • Does the zombie spawn at all (is it in the hierarchy)
  • Which methods exactly do the buttons invoke, for example: [1]: https://i.stack.imgur.com/9xbgy.png
  • You're using a singleton pattern but you don't change scenes in this class. Are you changing scenes in any of your scripts? If yes, you will have to consider looking into them because every script that is persisting between scenes with the DontDestroyOnLoad() method could potentially interfere with your player.

Vlad

Related