• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Sep 05, 2020 at 11:29 AM by Azeraia for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Azeraia · Sep 03, 2020 at 09:59 PM · animatordestroydontdestroyonloadbegginer

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.

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_MXGApNcapNfNLg · Sep 04, 2020 at 10:19 AM 1
Share

Please paste the code you used to do this.

avatar image Azeraia unity_MXGApNcapNfNLg · Sep 04, 2020 at 07:46 PM 0
Share

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);       
 }

alt text

untitled4.jpg (94.0 kB)
avatar image Azeraia unity_MXGApNcapNfNLg · Sep 04, 2020 at 07:49 PM 0
Share

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

avatar image BenWiller1989 · Sep 04, 2020 at 08:12 PM 0
Share

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.

avatar image Azeraia BenWiller1989 · Sep 04, 2020 at 08:29 PM 0
Share

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 alt text

untitled5.jpg (400.7 kB)
avatar image BenWiller1989 BenWiller1989 · Sep 04, 2020 at 09:12 PM 0
Share

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.

avatar image Azeraia BenWiller1989 · Sep 04, 2020 at 09:17 PM 0
Share

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 alt text

untitled8.jpg (290.1 kB)

2 Replies

  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 17 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Azeraia · Sep 04, 2020 at 09:14 PM 0
Share

unfortunetly it didnt work :/. It can be the problemfrom the code of the "Play again" button? alt text

untitled8.jpg (290.1 kB)
avatar image VoidVenom Azeraia · Sep 04, 2020 at 09:17 PM 1
Share

It doesn't look like it. Would you be able to post your entire Game$$anonymous$$anager script?

avatar image VoidVenom VoidVenom · Sep 04, 2020 at 09:20 PM 1
Share

@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!

Show more comments
Show more comments
avatar image
0

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.

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Azeraia · Sep 04, 2020 at 08:50 PM 0
Share

I hope that the screenshot's will be a good answer :/ alt text alt text

untitled6.jpg (263.3 kB)
untitled7.jpg (272.9 kB)
avatar image VoidVenom Azeraia · Sep 04, 2020 at 08:53 PM 0
Share

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.

avatar image Azeraia VoidVenom · Sep 04, 2020 at 08:56 PM 0
Share

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.

Show more comments
Show more comments

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

183 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to stop duplicates from dontdestroyonload 1 Answer

How to play a destroy animation? Brick Breaker/Arkanoid. 1 Answer

How to make a saved score destroy on load new scene 0 Answers

"Dont destroy on load" does not work at forst 2 Answers

Object.DontDestroyOnLoad ? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges