How to run a script twice in a scene
Hey,
I've got a scene and it's basically a store, and I got a script (in C#) that disables a canvas until the loading screen has finished, when I run it for the first time it works perfectly, but when I click a back button that I implemented in the game and go back to the store scene the script that disables the canvas does not run.
My only guesses are is that unity does not run a script when it's already been run, is there a way around that?
Thanks, Nathan.
Here's my script that does the job for me in case you need it:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Diagnostics;
public class ProgressBar : MonoBehaviour
{
public Image circularSilder;
public float time;
public Canvas loadingBar;
public Canvas playMenu;
public Canvas money;
private float loadingTime;
public float fadeTime;
public void Start()
{
loadingTime = Random.Range(1f, 4f);
StartCoroutine(Example());
circularSilder.fillAmount = 0f;
}
public IEnumerator Example()
{
print(Time.time);
yield return new WaitForSeconds(4);
//fade it out
CanvasGroup canvasGroup = GetComponent<CanvasGroup>();
while (canvasGroup.alpha > 0)
{
canvasGroup.alpha -= Time.deltaTime / fadeTime;
yield return null;
}
DestroyObject(loadingBar);
print("Loaded Store");
loadingBar.enabled = false;
playMenu.enabled = false;
money.enabled = true;
Time.timeScale = 1;
}
public void Update()
{
circularSilder.fillAmount += Time.deltaTime / loadingTime;
}
}
If the object remained in the scene, it won't be run again. You can call its Start method to start it up again.
That you for the answer but how would i call its start method everytime i go into that scene ?
So, this script is on an object that is in a scene you load, unload and reload ? Do you simply load the scene with the Scene$$anonymous$$anager?
Answer by RensDevolp · Dec 21, 2016 at 01:43 PM
my guess is that you have to call the ienumerator again
Thank you for the answer but how would i call its start method everytime i go into that scene ?
Answer by tiggy02 · Dec 21, 2016 at 03:06 PM
Hello Skypiggy,
Your problem is that you can only call the Start() method once per time the scene is loaded, you can use OnEnabled() to bypass this as it has nearly the same propertys as Start() (being that as soon as the gameObject and script are enabled it will run) but it can be called multiple times per scene load.
Or you could use a public void MethodNameHere() so that you can call the method from another script in the same scene or a button press.
Goodluck, from tiggy02.
Thank you i will try that, so i just replace the method 'Start()' to 'OnEnabled()'
@Skypiggy I am sorry I made a typo ins$$anonymous$$d on OnEnabled() you need OnEnable() without the "d"
Sorry for any inconvenience that may have caused.
Your answer
Follow this Question
Related Questions
Problem with menu 1 Answer
How to run a script twice in a scene 0 Answers
Can I change scripting runtime version in a middle of the project? 0 Answers
Adding additional C# scripts after build (Android, iOS) 1 Answer
how do i take whats on one UI canvas and make it apear on another in real time(for a card game) 0 Answers