- Home /
My GUITexture Script doesn't re-activate when reloading a level.
I have a Screen Fadeinout script that uses a gui tecxture in both my mainmenu and my level. when I load the level everything seems fine, until I go back to the main menu and everything is black. In the editor I noticed that I was indeed using my main menu, but for some reason my fade out script didn't re-launch. so looking through the edito window I selected start game, and loaded my level, same resault, but when I pause the game and resume it the script awakes and I can see the game again. I dunno what is causing the problem, here are my main menu script and my pause script, as well as my screen fade in out script:
Main Menu:
using UnityEngine;
using System.Collections;
public class MainMenu : MonoBehaviour {
public float inbetweenSpeed = 0.5f;
private int buttonIdentifier = 1; //this specifies wich button is selected
private bool isBegining = true;
//the ones bellow are just to identify the elements on the menu scene
private GameObject isStart;
private GameObject isAnyKey;
private GameObject isQuit;
public GameObject backgroundSphere;
public GameObject menuCursor;
Vector3 pos1 = new Vector3 (-0.7f,0.85f,-8.4f);
Vector3 pos2 = new Vector3 (-0.7f,0.4f,-8.4f);
private ScreenFadeInOut fade;
private bool gameStart = false;
void Awake()
{
//referencing my elements
isStart = GameObject.Find("txt_StartGame");
isAnyKey = GameObject.Find("txt_PressAny");
isQuit = GameObject.Find("txt_QuitGame");
fade = GameObject.Find ("ScreenFader").GetComponent<ScreenFadeInOut>();
}
void Start() //at the begining of the scene this elements will be hidden
{
isStart.renderer.enabled = false;
isQuit.renderer.enabled = false;
menuCursor.renderer.enabled = false;
isAnyKey.renderer.enabled = true;
}
void Update()
{
if (isBegining) //this is the start of the game
{
AnyKeyPressed();
}
else //this is the menu options
{
MenuOptions();
}
if(gameStart)
{
fade.fadeSpeed = 0.5f;
fade.EndScene();
}
}
void AnyKeyPressed()
{
if(Input.anyKey)
{
isBegining = false;
isAnyKey.renderer.enabled = false;
}
}
void MenuOptions()
{
isStart.renderer.enabled = true;
isQuit.renderer.enabled = true;
menuCursor.renderer.enabled = true;
SelectOption();
//menu options are revealed
}
void SelectOption()
{
if(buttonIdentifier == 1)//this is first button
{
isStart.renderer.material.color = Color.magenta;
isQuit.renderer.material.color = Color.gray;
menuCursor.transform.position = pos1;
//here im higlighting one of the buttons as well as positioning the cursor.
if(Input.GetKeyDown (KeyCode.DownArrow))//here one switches buttons
{
buttonIdentifier = 2;
}
if(Input.GetKeyDown (KeyCode.Return))//here we use this button
{
gameStart = true;
}
}
if(buttonIdentifier == 2)//this is second button
{
isStart.renderer.material.color = Color.gray;
isQuit.renderer.material.color = Color.magenta;
menuCursor.transform.position = pos2;
if(Input.GetKeyDown(KeyCode.UpArrow))
{
buttonIdentifier = 1;
}
if(Input.GetKeyDown (KeyCode.Return))
{
Application.Quit ();
}
}
}
}
Pause Menu:
using UnityEngine;
using System.Collections;
public class PauseMenu : MonoBehaviour
{
private GameObject title;
private GameObject resume;
private GameObject quitOption;
private bool pause = false;
private int buttonIdentifier = 1;
void Awake()
{
title = GameObject.Find("txt_Title");
resume = GameObject.Find ("txt_Resume");
quitOption = GameObject.Find ("txt_Quit");
}
void Start()
{
title.renderer.enabled = false;
resume.renderer.enabled = false;
quitOption.renderer.enabled = false;
}
void Update()
{
if(Input.GetKeyDown(KeyCode.Escape))
PauseMenu();
SelectButton();
}
void PauseMenu()
{
pause = !pause;
if(pause)
{
title.renderer.enabled = true;
resume.renderer.enabled = true;
quitOption.renderer.enabled = true;
Time.timeScale = 0;
buttonIdentifier = 1;
SelectButton();
}
else
{
title.renderer.enabled = false;
resume.renderer.enabled = false;
quitOption.renderer.enabled = false;
Time.timeScale = 1;
}
}
void SelectButton()
{
if(Input.GetKeyDown(KeyCode.DownArrow))
{
buttonIdentifier = 2;
Debug.Log("select button was pressed");
}
if(Input.GetKeyDown(KeyCode.UpArrow))
{
buttonIdentifier = 1;
Debug.Log("select button was pressed.");
}
if(buttonIdentifier == 1)
{
resume.renderer.material.color = Color.magenta;
quitOption.renderer.material.color = Color.gray;
if(Input.GetKeyDown(KeyCode.Return))
PauseMenu();
}
if(buttonIdentifier == 2)
{
resume.renderer.material.color = Color.gray;
quitOption.renderer.material.color = Color.magenta;
if(Input.GetKeyDown(KeyCode.Return))
Application.LoadLevel(0);
}
}
}
Fade In Out Script:
using UnityEngine;
using System.Collections;
public class ScreenFadeInOut : MonoBehaviour
{
public float fadeSpeed = 1.5f;
public int levelNumber = 0;
private bool sceneStarting = true;
void Awake()
{
guiTexture.pixelInset = new Rect(0f, 0f, Screen.width, Screen.height);
}
void Update()
{
if(sceneStarting)
StartScene();
}
void FadeToClear()
{
guiTexture.color = Color.Lerp (guiTexture.color, Color.clear, fadeSpeed * Time.deltaTime);
}
void FadeToBlack()
{
guiTexture.color = Color.Lerp (guiTexture.color, Color.black, fadeSpeed * Time.deltaTime);
}
void StartScene()
{
FadeToClear();
if(guiTexture.color.a <= 0.05f)
{
guiTexture.color = Color.clear;
guiTexture.enabled = false;
sceneStarting = false;
}
}
public void EndScene()
{
guiTexture.enabled = true;
FadeToBlack();
if(guiTexture.color.a >= 0.95f)
{
Application.LoadLevel (levelNumber);
}
}
}
Hope some one sees the problem, cause I don't D:
Answer by Mesmetron · Oct 11, 2013 at 10:24 PM
Hi NoaB,
The reason is that you place the start of the fade process inside the awake function which runs only once when the script is loaded, it is not related with the number of times you load a scene.
To solve the problem you have to find a way to know that the scene you are currently runing is diferent from the last or that it was just loaded, this can be achived in many ways, here are two you migth find usefull:
-use Application.lodedLevel : this will tell you the index of the current scene, when this number change you can start the fade process.(this migth not work when the main scene is loaded the first time but it could be usefull in other situations)
-use Time.timeSinceLevelLoad : this will tell you the number of seconds since this level was loaded, just use something like this:
if(Time.timeSinceLevelLoad < 1 && !faded) {
faded = true;
"fade function";
}
Every time you start to load a scene remember to set the faded boolean to false again, so the fade function ocurs.
I hope you find my answer helpfull, Good Luck!
I dont seem to understand, the start of the fade process is in the ScreenFadeInOut script, and the whole process itself is located in the Update function. in the $$anonymous$$ain $$anonymous$$enu script, the script is being referenced to the fade variable in the awake function, nothing is being affected there. Though it does seem to make sense to use the if condition you just gave, but in what function should I put it? Awake or Update?
Sorry NoaB, I was a little bit lost the first time I saw your post.
The function is well placed in Update just as you have it, but I think i just found the reason, in the fadeInOut script there is a boolean called "sceneStarting" in the begining its == true so the fade function works fine the first time its loaded, but it is not reseted to true before loading back to scene 0 in line 88 of Pause $$anonymous$$enu.
I hope this time it will work :)
Thanks $$anonymous$$esmetron!! going to try to turn it to true again, Oh also, I noticed that its not just my GUI texture that stops, but all animation in the scenes as well stop, even the ones set to loop. Any ideas or would the solution be the same?
thanks man. I dont know why this never hit me. Wasted 2 hours on it. Thanks a lot
Answer by NoaB · Oct 13, 2013 at 08:11 PM
Never Mind, Found the root of the problem, my pause menu script. Turns out that Time.timeScale doesn't reset on its own, so when I was going back to my main menu from the pause menu, I was returning with a time scale of 0!! I changed my script so that TimeScale resets back to 1 and everything is working now!!
Im glad to hear that, good look with your project NoaB!
Your answer
Follow this Question
Related Questions
Can't go back to the game scene from main menu for the second time 2 Answers
Play Scene help 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Problems with pause & main menu. PLEASE HELP :( 1 Answer
reset the level? 5 Answers