Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Vulture_6 · Sep 07, 2016 at 05:32 PM · scripting problemcanvasscripting beginner

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

Comment
Add comment · Show 9
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 TBruce · Sep 07, 2016 at 06:54 PM 1
Share

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.

avatar image Vulture_6 TBruce · Sep 07, 2016 at 10:32 PM 0
Share

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

avatar image doublemax · Sep 07, 2016 at 11:04 PM 0
Share

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.

avatar image Vulture_6 doublemax · Sep 08, 2016 at 09:00 AM 0
Share

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

alt text

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.

avatar image doublemax Vulture_6 · Sep 08, 2016 at 10:37 PM 1
Share

@Vulture_6

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

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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

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
avatar image
0

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

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 Vulture_6 · Sep 07, 2016 at 10:25 PM 0
Share

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?

01-hierachy.png (68.9 kB)

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

87 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How to position world space canvas from script 0 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