Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 NoaB · Oct 11, 2013 at 08:59 PM · guitexturepauseloadlevelmainmenu

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:

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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!

Comment
Add comment · Show 4 · 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 NoaB · Oct 12, 2013 at 03:50 AM 0
Share

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?

avatar image Mesmetron · Oct 12, 2013 at 10:47 PM 0
Share

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 :)

avatar image NoaB · Oct 13, 2013 at 07:37 PM 0
Share

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?

avatar image tobicreaper · Jan 21, 2014 at 06:17 AM 0
Share

thanks man. I dont know why this never hit me. Wasted 2 hours on it. Thanks a lot

avatar image
0

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!!

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 Mesmetron · Oct 13, 2013 at 11:19 PM 0
Share

Im glad to hear that, good look with your project NoaB!

avatar image
0

Answer by tolgaclk · Oct 14, 2014 at 12:08 PM

alt text


screenshot_3.jpg (33.7 kB)
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

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

19 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

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


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