D
D
DickCrow2020-10-04 03:00:21
Unity
DickCrow, 2020-10-04 03:00:21

When the scene is restarted, the script retains the past values, rather than updating completely. What to do?

I wrote a small code so that objects gradually accelerate over time. And now, when I die, I press restart to restart the scene, and the objects for the place to start picking up speed again, they continue to fall at the same speed. Tell me what can be done.
Here is the code for objects that should speed up little by little:
[SerializeField]
private static float fallSpeed ​​= 3f;

void Start()
{
StartCoroutine(Accelerate());
}
IEnumerator Accelerate()
{
while (true)
{
fallSpeed ​​= fallSpeed ​​+ 0.01f;
yield return new WaitForSeconds(20f);
}
}
void Update()
{
if (transform.position.y < -6f)
Destroy(gameObject);

transform.position -= new Vector3(0, fallSpeed ​​* Time.deltaTime, 0);
}

On the restart button, the usual code is:
void OnMouseDown()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2020-10-04
@GavriKos

private static float fallSpeed ​​= 3f;

Because static

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question