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 /
avatar image
0
Question by JamesGryphon · Jun 12, 2017 at 08:59 AM · variablestringenabled

Using a String in an Enable statement

I'm working on a main menu that has a number of buttons that the user can press at any time, switching from one panel to another. I thought it'd save time and some speed, as well as be much more elegant, to keep track of which button the user most recently pressed, and then simply turn that one off when switching to a different panel, rather than disabling all of them every time. So I wrote this:

 public class MainMenu : MonoBehaviour {
     public Canvas MenuCanvas;
     public Canvas FirstCanvas;
     public Canvas StartCanvas;
     public Canvas JoinCanvas;
     public Canvas PrefsCanvas;
     public Canvas GuideCanvas;
     public Canvas StatsCanvas;
     public Canvas MusicCanvas;
     public Canvas SoundCanvas;
     string CanvasName; 
 
     void Awake()
     {
         MenuCanvas.enabled = true;
         FirstCanvas.enabled = true;
         CanvasName = "FirstCanvas";
     }
 
     public void JoinOn()
     {
         CanvasName.enabled = false;
         JoinCanvas.enabled = true;
         CanvasName = ("JoinCanvas");
 }
 

The problem is, I can't figure out how to get CanvasName to return its value, and to use that as part of the enable statement. It's obvious why CanvasName.enabled won't work; it's trying to turn off the string itself and not reacting to the value of that string, but knowing that doesn't help me much.

If anyone has any ideas about how to make this work, or better suggestions for how to implement this kind of activity tracking, I'd be grateful.

Comment
Add comment · Show 1
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 revolute · Jun 12, 2017 at 09:06 AM 0
Share

Well, you can just keep a reference to the last canvas activated.

public Canvas lastCanvas;

Somewhere along the line

lastCanvas = something;

That would do the trick.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by JamesGryphon · Jun 16, 2017 at 05:22 PM

I worked out a solution based on some other people's posts, using the dictionary method, and thought I'd include a sample to help anyone else that stumbles across this out. I haven't tried rev's proposal, but readers are welcome to give it a go for themselves and see how it works if they don't want to go to this much trouble.

public class Sample : MonoBehaviour {

public Canvas FirstCanvas;

public Canvas JoinCanvas;

Dictionary myCanvases = new Dictionary ();



void Awake()
 {

FirstCanvas.enabled = true;

myCanvases[0] = FirstCanvas;

}



public void JoinOn()
 {

myCanvases [0].enabled = false;

JoinCanvas.enabled = true;

myCanvases[0] = JoinCanvas;
 }


}

I don't promise that it's the easiest or best solution, but it is definitely workable; I have it running in my own main menu.

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 webcam · Jun 16, 2017 at 05:50 PM

You can accomplish this same result without the dictionary here. Currently, you have references to the two canvas you want to enable and disable and you can just use those. If you want to use a string as a key a dictionary makes sense but the way you have it working now you can just simplify it to use your references.

public class Sample : MonoBehaviour {
 public Canvas FirstCanvas;
 public Canvas JoinCanvas;


  void Awake()

 {  
  
      FirstCanvas.enabled = true;
 
 }



  public void JoinOn()
 
  {
      FirstCanvas.enabled = false; 
  
      JoinCanvas.enabled = true;
 
  }

}

Comment
Add comment · Show 2 · 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 JamesGryphon · Jun 16, 2017 at 05:55 PM 0
Share

Well, the sample is a reduced version of the full script, for proof of concept. The actual main menu has something like eight different canvases, hence the need to keep track of what's going on.

avatar image webcam JamesGryphon · Jun 19, 2017 at 03:13 PM 1
Share

Okay fair enough let's look at the dictionary then. What you want to do with a dictionary is create a string key and a corresponding canvas value. Something like this

  public class $$anonymous$$ain$$anonymous$$enu : $$anonymous$$onoBehaviour 
  {
        Dictionary menus = new Dictionary<string, Canvas>();

        public Canvas $$anonymous$$enuCanvas;
        public Canvas FirstCanvas;
        public Canvas SecondCanvas;
       public Canvas enabledCanvas;
      void Awake()
      {
          menus.Add("$$anonymous$$enuCanvas", $$anonymous$$enuCanvas);
          menus.Add("FirstCanvas", FirstCanvas);
          menus.Add("SecondCanvas", SecondCanvas);
      }
 
      public void SetCanvasActive(string key)
      {
          enabledCanvas.enabled = false;
          enabledCanvas = menus[key];
           enabledCanvas.enabled = true;
      }
  }


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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

string + int = variable 2 Answers

Text UI is not being assigned 2 Answers

Converting 'char' to the name of a variable 1 Answer

Change multiple variables with function argument 2 Answers

Public variable hidden in the inspector 2 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