0

I want access to a component of a UI Button and want to enable a disabled component " Button(Script)" for unlocking the next level but it is not enable the component.

if(PlayerPrefs.GetInt("Level") == 1){
   Button Level2 = Unlock.GetComponent<Button>();
   Level2.enabled = true;
}

P.S: No Syntax Error in my code.

4
  • 1
    Debug the code then edit your question with the result. It's hard to help you without that. Check if Level2 is null by putting Debug.Log(Level2) inside that if statement.. This will also tell you if that if that statement is even running. Commented May 8, 2017 at 7:28
  • You can also try to set the interacrable attribute from a button to false or true. Commented May 8, 2017 at 7:52
  • Thank you @cjf93 your comment save my time.. Commented May 8, 2017 at 10:25
  • i posted as answer, maybe it works for someone else Commented May 8, 2017 at 11:47

2 Answers 2

3

You can also set the interactable property of a Button to true o false.

Button myButton;
myButton.interactable = true;
//Here your button works normal

myButton.interactable = false;
//The click on your button is disabled here
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks I have already solved the problem with same method you have mentioned
I will add some thing for others that you should not disable whole component but use the variable of the component which act as whole component enable/disable option.
0

Make sure the if block is executed , make sure your "Unlock" -gameobject I guess- has a button component on it. No syntax error doesn't mean you don't have conceptual error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.