Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Texture on a GUI.Button disappears when scene is reloaded

Discussion in 'Scripting' started by Exnihilon, May 6, 2014.

  1. Exnihilon

    Exnihilon

    Joined:
    Mar 2, 2014
    Posts:
    157
    Hello forum. I have a little problem with a GUI.Button and the reloading of a scene. Let me explain the situation.

    In my application there is one scene. At that scene, there are some GUI.Buttons that when clicked, some scripts attached to other GameObjects are run.

    Among those GUI.Buttons there is one named "Animator" (btnTexture1), that on click, enables the 2nd camera that sees an animation on a texture of a GameObject.

    There is also another GUI.Button, named "Reload", that when clicked, reloads the scene, so every GameObject variable is set to initial state. Here is the code snippet for the button:

    Code (csharp):
    1.  
    2.  
    3. // That button reload the level named "Scene1"
    4. if (!btnTexture2)
    5.              {
    6.             Debug.LogError("Please assign a texture on the inspector");
    7.             return;
    8.             }
    9.            
    10.             if(GUI.Button(Rect(25, 180, 30, 15), btnTexture3))
    11.             {
    12.             Application.Quit();
    13.             Application.LoadLevel ("Scene1");
    14.             }
    15.  

    The problem that I'm facing here, is that the texture asigned to the GUI.Button named "Animator"( btnTexture1), disappears when the "Reload" button is pressed.

    Here is the code snippet for the btnTexture1:

    Code (csharp):
    1.  
    2.  
    3. #pragma strict
    4.  
    5.     var native_width :  float = 480;
    6.     var native_height : float = 320;
    7.     var btnTexture1 : Texture;
    8.     var btnTexture2 : Texture;
    9.     var animaCam1 : Camera;
    10.     var animaCamOn : boolean = false;
    11.     public var runAnimation = false;
    12.      
    13.     function Start()
    14.     {
    15.         animaCam1.enabled = false;
    16.     }  
    17.    
    18.     // Enable - disable the camera that sees the animation on a GameObject (texture)
    19.  
    20. function Update()
    21.     {
    22.         if (!animaCamOn)
    23.         {
    24.             animaCam1.enabled = false;
    25.         }
    26.         else
    27.         {
    28.                animaCam1.enabled = true;
    29.         }
    30.     }
    31.  
    32.     function OnGUI () {
    33.      
    34.         //set up scaling
    35.         var rx : float = Screen.width / native_width;
    36.         var ry : float = Screen.height / native_height;
    37.      
    38.         //The GUI.matrix will scale everything automatically.
    39.         GUI.matrix = Matrix4x4.TRS (Vector3(0, 0, 0), Quaternion.identity, Vector3 (rx, ry, 1));
    40.      
    41.        // That button switches on/off the Camera  2, and executes the animation on a gameobject
    42.         if (!btnTexture1) {
    43.             Debug.LogError("Please assign a texture on the inspector");
    44.             return;
    45.         }
    46.  
    47.         if(GUI.Button(Rect(10, 180, 30, 15), btnTexture1)) {
    48.             animaCamOn = !animaCamOn;
    49.             runAnimation = !runAnimation;
    50.         }
    51.     }
    52.  
    After the scene is reloaded, although the texture asigned to the "Animator" button has disappeared, the button itself stays functional.

    Has anyone came upon the same problem, and what might be a workaround?

    I 'm new to Unity and programming to cope up with such a problem, so anyone kind enough please respond.
    Thank you all in advance for your answers.
     
  2. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    490
    You'll want to remove Application.Quit(); from that button. Inside unity it won't do anything but once you've built it into an executable that code will cause the game to close.

    I placed both of your code snippets into scripts on an empty game object and didn't have any issues with the texture disappearing. It stayed on the button after the level reloaded, both visibly and in the inspector. Perhaps some other code you haven't posted is altering it?
     
  3. Exnihilon

    Exnihilon

    Joined:
    Mar 2, 2014
    Posts:
    157
    Hi @QuinnWinters. I don't know why the texture is dissapearing, on reloading. The problem is that, I used to have a "GUI Controller" script, that rendered all the gui.buttons with the funtionality scripts attached to those buttons.

    I had to alter this logic, only for the button that does the animation on the texture,http://forum.unity3d.com/threads/24...-every-time-a-gui-button-is-clicked?p=1614458 as I couldn't implemented it, as a working code to the forementioned "GUI Controller" script.

    Is my coding looking good for the scene reloading, or should be altered? All I wanted to do, is to return all variables of the game.objects to initial state.

    Furthermore, I thought off a workaround that would do the trick. Having the scene loaded with all the game.objects as static, and then instanciate it with all the game.objects in non static state, so the scripts I have attached to the gui.buttons, could work.
    I don't know though, how to code this idea, or if it would compile.

    EDIT 1

    I forgot to mention that, the texture is visible in Inspector before and during play in Unity3d.
    Also, till now the texture used to be visible in player window on start and disappeared on scene reloading, now it isn't visible on start.
     
    Last edited: May 7, 2014