- Home /
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...
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);
}
}
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.
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.
Check if levelButton is found by using a Debug.Log.
Put:
Debug.Log(levelButton);
after line:
levelButton = GameObject.Find(n.ToString ());
I accidentally opened with the old editor and it wiped my menu scene. Thanks anyway. :(
Your answer
![](https://koobas.hobune.stream/wayback/20220613195625im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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