- Home /
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;
}
}
}
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
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?
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;
}
}
}
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);
}
}
@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
Follow this Question
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