Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Wulf_BiAS · May 15, 2015 at 07:10 AM · guibutton

Set Buttons to Interactable within C#

I feel like I am missing something simple... but basically I have a menu where the player picks level 1-36. Each button is names after the level it loads... 1 is 1, 13 is 13...

I setup player prefs to store a variable when loading levels... Load level 5, if playerpref of UnlockedLevel<5, set to 5... That bit works.. even hitting delete and setting it back to 1 works (according to some debug outputs).

Now when the menu loads I believe I should be able to loop through from 1 to n where n is playerprefs UnlockedLevel value.

 void LoadMainMenu()
 {
     Button levelButton;
     Application.LoadLevel("Main");
     //unlock Levels from Player Prefs
     int UnlockToLevel = PlayerPrefs.GetInt ("UnlockToLevel");
     for(int n = 1; n <=  UnlockToLevel; n++)
     {
         levelButton = GameObject.FindGameObjectsWithTag (n.ToString ());
         levelButton.SetActive(true);
     }

I know UnlockToLevel is being set to the proper number. This is my first attempt at a for loop, but I believe the loop itself is correct.

I am sure my issue is saying "let's grab button n". I tried it with "3" instead of n and the error for that line disappears but button 3 isn't being unlocked. So should I somehow cast the int to a string? Am I even going about this the right way?

Thanks!

EDIT: updated code to show what I have changed to. I think i'm getting closer...

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by HarshadK · May 15, 2015 at 07:22 AM

There are a few things here and there in your script. Check comments in the code below to understand what has changed.

 void LoadMainMenu()
 {
     // Changed the type of levelButton to GameObject since that is what you are using in your for loop
     GameObject levelButton;
     // This line is commented out since you don't want to load level before here.
 //         Application.LoadLevel("Main");
          //unlock Levels from Player Prefs
     int UnlockToLevel = PlayerPrefs.GetInt ("UnlockToLevel");
     for(int n = 1; n <=  UnlockToLevel; n++)
          {
         // Replaced FindGameObjectsWithTag by Find since you need to find your gameobject by name and not tag as your n is the name of the gameobject
              levelButton = GameObject.Find(n.ToString ());
              levelButton.SetActive(true);
          }
 }

Comment
Add comment · Show 4 · 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 Wulf_BiAS · May 15, 2015 at 08:00 PM 0
Share

Thanks for assisting. I will try these when I get a moment. Should I be processing level unlocks separately from loading the level? I want this to happen once the main menu is loaded.

avatar image Wulf_BiAS · May 21, 2015 at 11:00 PM 0
Share

while that compiles it unfortunately leaves me with this error: NullReferenceException: Object reference not set to an instance of an object PlayerController.Load$$anonymous$$ain$$anonymous$$enu () (at Assets/Scripts/PlayerController.cs:279)

There has to be something straight forward about GUI buttons that I am just missing.

avatar image HarshadK · May 22, 2015 at 05:32 AM 0
Share

Check if levelButton is found by using a Debug.Log.

Put:

 Debug.Log(levelButton);

after line:

 levelButton = GameObject.Find(n.ToString ());
avatar image Wulf_BiAS · May 28, 2015 at 04:07 PM 0
Share

I accidentally opened with the old editor and it wiped my menu scene. Thanks anyway. :(

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

2 People are following this question.

avatar image avatar image

Related Questions

Problem with UI in iOS / Android 1 Answer

GUI does not contain a definition for `skin' ? 1 Answer

GUI Button within if statement doesn't disappear even when the if statement is false 2 Answers

How to Disable GUI Button Immediately After Click 1 Answer

How to detect if a button is continuosly being PRESSED? 1 Answer


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