Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by Morice1999 · Jun 26, 2018 at 01:02 PM · startdontdestroyonloadawake

Keeping a variable after reloading a scene

Hello, i have a variable to controll my animation speed and i want to save that even if i reload the scene so i used a gameobject with DontDestroyOnLoad() with the script attached to it but it just does not work like i want it to. Im relatively new to Unity and C# so dont blame me for stupid code or mistakes. Thank you in advance.

The Script that needs the Variable:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class AnimationSpeed : MonoBehaviour
 {
     public AnimationSpeedRequierments ASR;
     Animator m_Animator;
 
     void Start()
     {
         m_Animator = gameObject.GetComponent<Animator>();
     }
 
     void OnGUI()
     {
         if (ASR.m_buttonPressed3Times)
         {
             Debug.Log("ASR = " + ASR.animationSpeedFloat);
             GUI.Label(new Rect(20, 10, 220, 100), "Geschwindigkeits Multiplikator 0 bis 4");
 
             ASR.animationSpeedFloat = GUI.HorizontalSlider(new Rect(250, 15, 400, 60), ASR.animationSpeedFloat, 0.0F, 4.0F);
 
             m_Animator.speed = ASR.animationSpeedFloat;
         }
     }
    
 }


The Script that holds the Variable:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class AnimationSpeedRequierments : MonoBehaviour {
 
     private float m_buttonPressed;
     [HideInInspector]
     public bool m_buttonPressed3Times;
     public AnimationSpeed AS;
     [HideInInspector]
     public float animationSpeedFloat;
 
     void Start()
     {
         animationSpeedFloat = 1.0F;
     }
 
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.Z))
         {
             m_buttonPressed++;
             Debug.Log("Speedbutton pressed" + m_buttonPressed + "x");
         }
         if (m_buttonPressed == 3)
         {
             m_buttonPressed3Times = true;
         }
         else if (m_buttonPressed > 3)
         {
             m_buttonPressed3Times = false;
             m_buttonPressed = 0;
         }
     }
 }
 

Comment
Add comment · Show 2
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 Hellium · Jun 26, 2018 at 01:12 PM 1
Share

Where do you call DontDestroyOnLoad? Who calls it?

The problem with DontDestroyOnLoad is that if your scene is loaded again, you will have two instances of AnimationSpeedRequierments

avatar image Morice1999 Hellium · Jun 26, 2018 at 01:23 PM 0
Share

@Hellium

The parent object has a script with DontDestroyOnLoad(). You described my problem do you know how to prevent it from being loaded again so it does not resets the variable over and over again?

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Morice1999 · Jun 26, 2018 at 02:39 PM

I found it out, here is the solution. Object with the Variable:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class AnimationSpeedRequierments : MonoBehaviour {
 
     private AnimationSpeed AS;
     [HideInInspector]
     public float animationSpeedFloat;
 
     public void Awake()
     {
         DontDestroyOnLoad(this);
 
         AS = Object.FindObjectOfType<AnimationSpeed>();
 
         if (FindObjectsOfType(GetType()).Length > 1)
         {
             Destroy(gameObject);
         }
     }
 
     void Update()
     {
         if(AS.sliderValue != 1.0F)
         {
             animationSpeedFloat = AS.sliderValue;
         }
     }
 }


Object with the Animation:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class AnimationSpeed : MonoBehaviour
 {
     private int m_buttonPressed;
     [HideInInspector]
     public bool m_buttonPressed3Times;
 
     private AnimationSpeedRequierments ASR;
     Animator m_Animator;
     [HideInInspector]
     public float sliderValue = 1.0F;
 
     void Start()
     {
         ASR = Object.FindObjectOfType<AnimationSpeedRequierments>();
         m_Animator = gameObject.GetComponent<Animator>();
 
         if(ASR.animationSpeedFloat != 1.0F)
         {
             sliderValue = ASR.animationSpeedFloat;
         }
     }
 
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.Z))
         {
             m_buttonPressed++;
             Debug.Log("Speedbutton pressed" + m_buttonPressed + "x");
         }
         if (m_buttonPressed == 3)
         {
             m_buttonPressed3Times = true;
         }
         else if (m_buttonPressed > 3)
         {
             m_buttonPressed3Times = false;
             m_buttonPressed = 0;
         }
     }
 
     void OnGUI()
     {
         if (m_buttonPressed3Times)
         {
             GUI.Label(new Rect(20, 10, 220, 100), "Geschwindigkeits Multiplikator 0 bis 4");
 
             sliderValue = GUI.HorizontalSlider(new Rect(250, 15, 400, 60), sliderValue, 0.0F, 4.0F);
 
             m_Animator.speed = sliderValue;
             ASR.animationSpeedFloat = sliderValue;
         }
     }
    
 }

Comment
Add comment · 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
1

Answer by bolkay · Jun 26, 2018 at 01:17 PM

I don't know what you've tried but your code in the applicable script should look like this...

  public static AnimationSpeedRequirements Instance{get {return instance;}}
     public static AnimationSpeedRequirements instance;
     void Start(){
     if(instance==null){
     instance=this;
     DontDestroyOnLoad();
     }
     else{
     DestroyImmediate(this.gameObject);
     }
     }
 
 
Comment
Add comment · Show 1 · 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 Morice1999 · Jun 26, 2018 at 02:12 PM 0
Share

@bolkay I did this so the new objects are getting deleted but my core Problem is still there. It loses the reference to this script because the object with the animation and the other script is getting reloaded. Is there a way to say that it has to search for this script again to use the variables ?

  public void Awake()
         {
             DontDestroyOnLoad(this);
     
             if (FindObjectsOfType(GetType()).Length > 1)
             {
                 Destroy(gameObject);
             }
         }




Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

87 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

Related Questions

DontDestroyOnLoad() not calling awake/start again 2 Answers

Start and Awake running twice? 2 Answers

Function that triggers something as soon as Object is "Set Active" 3 Answers

ExecuteInEditMode Component construct "twice" and "reset" custom classes 0 Answers

awake and start functions not working 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges