Camera script with 5 layers has strange behaviour on Unity play and on android device

Hello to all. I need a little help to solve out a problem with a camera script and a GUI.button menu.

Here is my scene hierarchy:

There is a camera (named “animaCam”) that can see 5 layers. On each layer there is a texture animation script attached to a gameobject (Cube) that plays an animation if a suitable gui.button is clicked, on a gui.box (Menu).

Before this camera, there is another camera (named “logoCam”), that serves as a logo screen, that sees one layer only.

With this configuration, I want to achieve the following functionality. When the user clicks a gui.button, a gui.box pops up, showing initially the logoCam and five gui.buttons to choose from.

Each of these buttons, does the following actions (with some misuses)

a. Should hide the logoCam
b. Should enable the animaCam
c. Should choose the suitable layer with the suitable Cube
d. Should start the animation script on the Cube

With this configuration, I get the app running on Unity player and on an android device, but I have to solve the following mulfunctions:

a. Once the user clicks the gui.button for the “showMenuOptions”, on the android device the menu stays on. Even when the button is clicked again to close it stays on screen. On Unity player, the gui.button for menu behaves as it should, open/closes on click (see image 1 image 2)

b. On user option to play e.g. the first animation, the function executes and the animation plays (see image 3).

When the user clicks the other gui.button that enables/disables the “showMenuOption”, the menu closes, but the animation keeps playing. It should be closed with the menu shut down button (see image 4), meaning that animaCam, logoCam and runAnima1 should be off in Inspector.

I believe, the error must be in the Update function. I have tried everything I could think of, without much success.

Please, take a look at the following code. Post in some thoughts or a even better a code snippet if I have mistaken something.
I really need to make the camera and gui.button menu, behave as it should, both on device an on Unity player.
Thank you in advance for your answers.

Here is the code snippet:

//ShowAnimation Menu with camera option

#pragma strict

    var native_width :  float = 480;
    var native_height : float = 320;
    var btnTexture : Texture;
    var bgrImage : Texture; 
    public var animaCam : Camera;
    public var logoCam : Camera;
    public var animaCamOn : boolean = false;
    public var logoCamOn : boolean = false;
    var showMenuOptions:boolean = false;    //true = show the other GUI elements
    public var runAnima1 = false;
    public var runAnima2 = false;
    public var runAnima3 = false;
    public var runAnima4 = false;
    public var runAnima5 = false;
    var animator1Mask : LayerMask; // select desired camera layer in inspector
    var animator2Mask : LayerMask; // select desired camera layer in inspector
    var animator3Mask : LayerMask; // select desired camera layer in inspector
    var animator4Mask : LayerMask; // select desired camera layer in inspector
    var animator5Mask : LayerMask; // select desired camera layer in inspector
       
    function Awake () 
    {
    DontDestroyOnLoad (GameObject.FindGameObjectWithTag("animator"));
    }   
     
    function Start() 
    {
        animaCam.enabled = false;
    }
    
     
    function Update() 
    {
        if (!animaCamOn) 
        {
            animaCam.enabled = false;
        }
        else 
        {
           animaCam.enabled = true;
        }
        
         if (!logoCamOn) 
        {
            logoCam.enabled = false;
        }
        else 
        {
           logoCam.enabled = true;
        }
        
        if(!showMenuOptions)
        {
            showMenuOptions = false;
        }
        else
        {
            showMenuOptions = true;
        }
     }
    
     
    function OnGUI () 
    {
      //set up scaling
        var rx : float = Screen.width / native_width;
        var ry : float = Screen.height / native_height;
     
        GUI.matrix = Matrix4x4.TRS (Vector3(0, 0, 0), Quaternion.identity, Vector3 (rx, ry, 1));
     
        //now create your GUI normally, as if you were in your native resolution
        //The GUI.matrix will scale everything automatically.
        
                
        if (!btnTexture) // This is the Route button that triggers the "showMenuOption"
        {
            Debug.LogError("Please assign a texture on the inspector");
            return;
        }
            // Make sure we only call GUI.Window if showMenuOption is true.
           if(GUI.Button(Rect(100, 200, 40, 25), btnTexture)) 
            
            showMenuOptions = !showMenuOptions;
                    
            if(showMenuOptions)
            {
            // Make a background GUI box for showMenuOption
            GUI.Box (Rect (12,2,350,100),"Choose to play an animation:");
            GUI.DrawTexture(Rect(120,30,300,85), bgrImage);
           logoCamOn = !logoCamOn;                        // In this configuration showMenuOptions is on/off with logoCam activated as a preview.
                                                           
            // Make the first button. If it is pressed, Animation (1) will be executed
            if (GUI.Button (Rect (10,14,100,20), "Anima1")) 
                {
                animaCamOn = !animaCamOn;    // animaCamOn disabled. In this configuration showMenuOptions is on/off with logoCam activated. On "Anima1" btn clicked, logoCamera stays on.Error!!!
                 logoCamOn = !logoCamOn;
                animaCam.cullingMask = Animator1Mask;
                runAnima1 = !runAnima1;                
            }

        // Make the second button. If it is pressed, Animation (2) will be executed
               if (GUI.Button (Rect (10,36,100,20), "Anima2")) 
                {
                animaCamOn = !animaCamOn;
            logoCamOn = !logoCamOn;
                animaCam.cullingMask = Animator2Mask;
                runAnima2 = !runAnima2;
                }

        // Make the third button. If it is pressed, Animation (3) will be executed
            if (GUI.Button (Rect (10,56,100,20), "Anima3")) 
                {
                animaCamOn = !animaCamOn;
            logoCamOn = !logoCamOn;
                animaCam.cullingMask = Animator3Mask;
                runAnima3 = !runAnima3;                }

        // Make the forth button. If it is pressed, Animation (4) will be executed
            if (GUI.Button (Rect (10,88,100,20), "Anima4")) 
                {
                animaCamOn = !animaCamOn;
            logoCamOn = !logoCamOn;
                animaCam.cullingMask = Animator4Mask;
                runAnima4 = !runAnima4;
            }
            
            // Make the fifth button. If it is pressed, Animation (5) will be executed
            if (GUI.Button (Rect (10,110,100,20), "Anima5")) 
                {
                animaCamOn = !animaCamOn;
            logoCamOn = !logoCamOn;
                animaCam.cullingMask = Animator5Mask;
                runAnima5 = !runAnima5;
            }
        }
    }

Exnihilon,

Did you fix your issues or you still have problems.
Are you animation always playing in the background and depending of the layer you pick you show one or the other?

a) I have no idea, I didn’t develop yet on Android.
b) It seems that if you click on your “showMenu” (showMenuOptions ) after you clicked on one of your animation button, you don’t reset your cullingMask, so basically the menu disappear but the camera is still rendering one of your animation layers. You probably need to reset your layerMask to the default one, if you close your menu. you could potentially add a condition for your button here that checks to reset the layer:

if(GUI.Button(Rect(100, 200, 40, 25), btnTexture)){
     // add a condition to reset the layer
     if(!showMenuOptions)
          // Reset layer
// ...
}

Hope it helps.

Hi laurelhach. No haven’t fixed my issues yet. I believe you are right. I should add a condition to reset the layer. I have tried adding the following line of code:

// Reset layers
        	routeCam.cullingMask = !routeCam.cullingMask;

but I get an error “Error BCE0022: Cannot convert ‘boolean’ to ‘int’. (BCE0022)”.

What should I add, after the equation (=)? My default layer, or other layers the cam sees?

You can’t use = ! for an int.
If you want to change your culling mask you need to do something like that :

routeCam.cullingMask = 0;

Basically you are trying to use your int as a bool. (= ! means true becomes false or the opposite (false becomes true) )
So make sure you reset your cullingMask to the default one you are using at the beginning.

Thank you laurelhach for the explanation on using =!. In my posted code, there is a camera (animaCam), that before the user clicks the "showMenu (showMenuOptions) button, sees the default layer.
If I put the following code to revert the culling mask to the default layer, I get error “BCE0043: Unexpected token: default. (BCE0043)”

animaCam.cullingMask = default;

I have tried to put a var as “var default : LayerMask”, I get error “Error BCE0044: expecting EOF, found ‘var’. (BCE0044)”
I have tied also using:

animaCam.cullingMask = 0;
animaCam.cullingMask = 1;

Still no compilation, still cannot revert animaCam to the default layer. Any tips?

Take a look at my posted code on the begging of this thread, cause I have edited it, by adding the Inspector’s screenimages on each function that I want to achieve, sadly without the desired outcome.

Hello :slight_smile:

Ok so what you can try is to create a default camera layer.
and assign the default layer (I don’t know what is your default layer camera)

  var defaultCameraMask : LayerMask; // default camera layer

Then you need to do that :

 routeCam.cullingMask = defaultCameraMask;

I am not super experienced with camera layer, but the best way is probably to use the default var to reset your camera.
Btw, “default” is probably not a variable if you didn’t assign it in your declaration.

That should work this way.

Thank you again laurelhach for your tips. I’'ll try and see what I get in compilation. Can you take a review in my posted code, in the beginning of this thread, cause it is edited with some screen images of the Inspector on the problems with the functionality I have so far described. Maybe you could direct me to the way to solve and fix this issue, after looking at those images.