How to disable all canvas at the start of a scene
I´ve been dealing with an extremly frustrating problem for 2 weeks. The forum couldn´t help yet, so you guys are my last resort.
I have a AR-scene with several canvas, that are supposed to be invisible when the scene was loaded. This script is used to achieve that:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ToggleMenu : MonoBehaviour
{
// Canvas that appears when the user clicks on the cube
public Canvas fountaininfo;
// Switch for visible / not visible setting
public bool fountainswitch;
// Button, that closes the canvas / makes it invisible again
public Button fountainclose;
// ###################
// ## Settings at the start ##
// ###################
void Start()
{
// Assinging the Canvas to this variable
fountaininfo = fountaininfo.GetComponent<Canvas> ();
// visibility setting at the start
fountaininfo.enabled = false;
// setting of the switch
fountainswitch = false;
}
// #######################################
// ## Function, that makes the button visible on click ##
// #######################################
private void OnMouseDown()
{
if (fountainswitch == false)
fountaininfo.enabled = true;
fountainswitch = true;
}
// ################################
// ## Function that closes the canvas again ##
// ################################
public void fountainclose_Press()
{
fountaininfo.enabled = false;
fountainswitch = false;
}
}
However, when the scene has loaded, half of the total number of canvas are visible - although I turned them off in the script - what am I missing here? - I put plenty of hours into finding a solution but I just cant find it :(
If you want to have all canvases disabled/deactivated at game start I would suggest doing this in the in the inspector. This way all canvases will not be visible. You can then activate/deactivate them through code when needed.
If I did that in the inspector, I cant turn anything back on within the game :( Also: I have no inspector to turn the canvas back on once I deploy an .apk file :(
So you have 4 cubes, each of them has this script attached and each of them has a Canvas "assigned" that you dragged into the public variable?
Which of the canvases do not disappear?
Did you double-check that each cube got a different canvas assigned?
What about the 5th one, 99_Can?
fountaininfo = fountaininfo.GetComponent<Canvas> ();
That line doesn't make sense. If fountaininfo already is a Canvas, you don't need this line.
the 5th one "99_Can" is supposed to be visible all the time - it carries a button, that leads frome the AR-scene back to the main menu (its the button on the bottom right).
I cant see any logic behind this - when I investigating this issue with 8 canvas, the 4th, the 6th, the 7th and the 8th canvas was visible - canvas no. 1, 2, 3, and 5 were fine.
fountaininfo = fountaininfo.GetComponent<Canvas> ();
Yeah, saw that too - the declaration was deleted. Ins$$anonymous$$d, I intend to assign the canvas manually in the inspector, therefore this line.
Ok, i checked the project, here's my analysis of the situation ;)
In my case the Start() routine for 01_Cube and 02_Cube was executed, but not for 03_Cube and 04_Cube. "Somebody" had deactivated these objects before their Start() routine was called.
The "AR Tracked Object" script turned out to be the culprit. In its Start() routine it deactivates all its children.
As a temporary fix you can move the Start() code in Toggle$$anonymous$$enu.cs to Awake() , so that this code is executed before the AR script kicks in. But as i don't know the "bigger picture" and the AR script, i can't tell you if that's a good solution.
I leave further research to you :)
One more comment about the code in On$$anonymous$$ouseDown()
if (BrunnenSchalter == false)
BrunnenInfo.enabled = true;
BrunnenSchalter = true;
Shall only the first line get executed when the condition is true or both?
It's really hard to tell if this code is intentional like it is, or if the programmer forgot some curly braces here.
P.S. Würzburg ist ne schöne Stadt, war ich auch schon mal. Grüße vom Niederrhein ;)
Answer by Vulture_6 · Sep 09, 2016 at 01:55 PM
@doublemax : himmelherrgott kruzifix, es funktioniert endlich! :D :D :D - Mit der Awake-Funktion, die vor der Startfunktion alles einstellt, klappt alles perfekt!
Vielen vielen vielen Dank! :D - Ich muss mal den debug-Befehl ausprobieren, um zu verstehen, wie du zu deiner Erkenntnis gekommen bist. Viele sonnige Grüße aus Würzburg zurück!
For all the others: Doublemax´ suggestion fixed this issue. I´ll try if switching the bool-condition within the OnMouseDown-Code is necessary. I only built it in to emulate a switch :D
Answer by HHammerite · Sep 07, 2016 at 06:50 PM
From what I can see it should work.. You mention multiple canvases though. Are they children of this canvas referenced in the script? If so then you could simply disable each child canvas as well.
foreach(Canvas i in fountaininfo.gameObject.GetComponentsInChildren<Canvas>())
{
//This is the canvas component and not the GameObject.
// To disable the GameObject you have to use yourGameObject.SetActive(bool);.
i.enabled = false;
}
In your script you are only disabling the canvas component. This won't have any affect on the children or that GameObject.
Hope this helps :)
Hello,
first of all, thank you for looking into this. Its right, i have multiple canvas - but they are all sitting at the root of the scene - please see attachment 01.
I also tried disabling the images and buttons, that are children of each canvas. Unfortunately it didnt change a thing - I still had canvas, that were visible once the scene was loaded.
Could we be dealing with a bug here?
Your answer
Follow this Question
Related Questions
Need help with two scrips 0 Answers
Issue with if-statements requiring two conditions. 1 Answer
Fading in Smoothly between Sprites 0 Answers
Image UI not enabling C# SOLVED 1 Answer