- Home /
The question is answered, right answer was accepted
Animator destroyed on DontDestroyOnLoad
Hello guys! Ho can help me please a bit? Every time i die in the game and i try to play again from the button "play again", everything works fine, but the Animator is destroyed. How i can make it work and not be destroyed? :/ I'm a begginer.
public enum GameState { Prepare, Playing, GameOver } public class Game$$anonymous$$anager : $$anonymous$$onoBehaviour
{ private Player motor;
private float timer = 8f;
public event System.Action<GameState> gameStateChanged = delegate { };
public static Game$$anonymous$$anager Instance { get; private set; }
private GameState _gameState = GameState.GameOver;
public bool IsDead { set; get; }
private bool isGameStarted = false;
public Animator /*gameCanvas*/ diamondAnim;
public Text scoreText,/* coinText,*/ modifierText, hiscoreText/*, totalCoinText*/;
private float score, modifierScore;
private int lastScore, coinScore, totalCoin, hiscore;
public Animator death$$anonymous$$enuAnim;
public Text deadScoreText, deadCoinText;
bool gameHasEnded = false;
private float restartDelay = (99f * 9999f) * 1999f;
public GameState GameState
{
get
{
return _gameState;
}
private set
{
if(value != _gameState)
{
_gameState = value;
gameStateChanged(_gameState);
}
}
}
public void Awake()
{
if(Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
motor = UnityEngine.GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
}
public void Start()
{
GameState = GameState.Prepare;
}
public void Update()
{
if (!isGameStarted)
{
isGameStarted = true;
//motor.StartRunning();
FindObjectOfType<FollowPlayer>().Is$$anonymous$$oving = true;
}
if (isGameStarted && !IsDead)
{
// Bump the score up
score += (Time.deltaTime * modifierScore);
if (lastScore != (int)score)
{
lastScore = (int)score;
scoreText.text = score.ToString("0");
Sound$$anonymous$$anager.instance.playScoreSound();
}
}
}
public void GameOver()
{
if(_gameState != GameState.GameOver)
{
StartCoroutine(CR_GameOver());
Ad$$anonymous$$anager.instance.showGameOverAd();
}
}
IEnumerator CR_GameOver()
{
deadScoreText.text = score.ToString("0");
deadCoinText.text = coinScore.ToString("0");
death$$anonymous$$enuAnim.SetTrigger("Dead");
// Check if this is a highscore
if (score > PlayerPrefs.GetInt("Hiscore"))
{
float s = score;
if (s % 1 == 0)
s += 1;
PlayerPrefs.SetInt("Hiscore", (int)s);
}
GameState = GameState.GameOver;
yield return new WaitForSeconds(3);
}
what you see in the above picture, is the result of pressing the play again button and only the animator is destroyed and i dont know how to not destroy the animator on load
I am sorry if I can't read your code, but where exactly did you assign the animator? You didn't just assign it manually, did you? That would explain things.
Sorry if i didnt understand what do you mean by " where did you assign the animator?"..I'm a noob and a begginer :( but if you mean this, here is a screenshot
Remove the else {Destroy (gameobject) ;}
and it'll work fine! You don't need to destroy the object, you want to keep for the new scene,... Unless I'm missing your point.
Unfortunetly it didnt work. Still is destroyed the Animator only, on pressing the Play Again button. Can be the problem with the script from the button? my game is in just one scene btw
Answer by VoidVenom · Sep 04, 2020 at 09:07 PM
Having looked closely at your Awake method, it appears that it may be setting itself as Instance when the first scene loads, then when you open another scene it calls Destroy(gameObject) because Instance is already set. Maybe this singleton pattern would work correctly? It's also important that not only do the animators not get destroyed, but neither does the game manager itself.
void Awake() {
if (Instance == null) {
Instance = this;
DoNotDestroyOnLoad(deathMenuAnim.gameObject);
DoNotDestroyOnLoad(diamondAnim.gameObject);
DoNotDestroyOnLoad(this.gameObject);
} else if (Instance != this)
Destroy(gameObject); // Maybe Destroy(this) would work better?
}
EDIT: fixed a missing semicolon in code.
EDIT 2: changed the code to represent the full answer, which was worked out in the comments of this answer.
unfortunetly it didnt work :/. It can be the problemfrom the code of the "Play again" button?
It doesn't look like it. Would you be able to post your entire Game$$anonymous$$anager script?
@Azeraia Oh my lord I think I've jut been a total moron this whole time. $$anonymous$$ake sure that you either reference the gameObject that the Animator is on, or the animator directly (not sure about the latter, though). I just realized you're not using the Game$$anonymous$$anager on the same object as your Animator. Sorry!
Answer by StonedLover · Sep 04, 2020 at 08:33 PM
On which Gameobject is the animator?
You can call DontDestroyOnLoad(Object obj) here you can insert the animator or the gameobject from the animator.
I hope that the screenshot's will be a good answer :/
This is probably a dumb question but did you save your script? I see the little * beside Game$$anonymous$$anager.cs indicating it hasn't been saved.
yup, it was saved, that was from a n atempt just to see if it changes something if i remove a thing, but nothing happened.