Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Button won't enable through Script

Discussion in 'Scripting' started by KaiXGT, Feb 18, 2021.

  1. KaiXGT

    KaiXGT

    Joined:
    Feb 12, 2021
    Posts:
    13
    Hi, I'm having some trouble enabling a button through code. I have an enabling and disabling function separated between 2 scripts, so I'll provide both.

    The enabling script is made to make sure the player moves forward. When they do, some text should disappear and a button should replace it, but it doesn't. In the Hierarchy, I see that it doesn't enable at all.

    Also I'm a big noob at this stuff


    Enabling:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6. public class TutorialManager : MonoBehaviour
    7. {
    8.     public GameObject PlayerBody;
    9.     public GameObject WalkForward;
    10.     public Button GoodJob1;
    11.  
    12.     void Update()
    13.     {
    14.         if (PlayerBody.transform.localPosition.z > 194.3748)
    15.         {
    16.             LoadNextTutorial();
    17.         }
    18.     }
    19.  
    20.     public void LoadNextTutorial()
    21.     {
    22.         WalkForward.SetActive(false);
    23.         GoodJob1.enabled = true;
    24.     }
    25.  
    26.     //IEnumerator TrashJoke()
    27.     //{
    28.     //    Sprinting.SetActive(false);
    29.     //    GoodJob4.SetActive(true);
    30.     //    yield return new WaitForSeconds(7);
    31.     //    GoodJob4.SetActive(false);
    32.     //    GoodJob405.SetActive(true);
    33.     //}
    34.  
    35.     //IEnumerator LoadMenu()
    36.     //{
    37.     //    ADS.SetActive(false);
    38.     //    FinishTutorial.SetActive(true);
    39.     //    yield return new WaitForSeconds(7);
    40.     //    SceneManager.LoadScene("Menu");
    41.     //}
    42. }
    Disabling:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class LoadNextTutorialFromGoodJob1 : MonoBehaviour
    7. {
    8.     public Button GoodJob1;
    9.     public GameObject WalkLeftBackwardRight;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.  
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.  
    21.     }
    22.  
    23.     public void LoadNextTutorial()
    24.     {
    25.         GoodJob1.enabled = false;
    26.         WalkLeftBackwardRight.SetActive(true);
    27.     }
    28. }
    Please help, thank you.
     
    Last edited: Feb 18, 2021
  2. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    Use Debug.Log to see your player's position, to be sure it's updating the way you think it should be. Also, I think it should be position, not localPosition (?), this depends on how you have things set up.
     
    KaiXGT likes this.
  3. KaiXGT

    KaiXGT

    Joined:
    Feb 12, 2021
    Posts:
    13
    Thank you, I’ll try this tomorrow and tell you if it works or not.
     
    seejayjames likes this.
  4. KaiXGT

    KaiXGT

    Joined:
    Feb 12, 2021
    Posts:
    13
    Okay, so I tried the things you've told me to do. The player's position is properly updating, but the button still won't enable. I'll keep on trying to figure this out.
     
  5. KaiXGT

    KaiXGT

    Joined:
    Feb 12, 2021
    Posts:
    13
    Update: I got it to appear, but it won't disappear anymore.

    I made the button a child of a GameObject and just replaced the button with it's parent in the Inspector.

    My scripts now look like this:

    Enable:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6. public class TutorialManager : MonoBehaviour
    7. {
    8.     public GameObject PlayerBody;
    9.     public GameObject WalkForward;
    10.     public GameObject GoodJob1;
    11.  
    12.     void Update()
    13.     {
    14.         if (PlayerBody.transform.position.z > 194.3748)
    15.         {
    16.             //Debug.Log(PlayerBody.transform.position.z);
    17.             LoadNextTutorial();
    18.         }
    19.     }
    20.  
    21.     public void LoadNextTutorial()
    22.     {
    23.         WalkForward.SetActive(false);
    24.         GoodJob1.SetActive(true);
    25.     }
    26.  
    27.     //IEnumerator TrashJoke()
    28.     //{
    29.     //    Sprinting.SetActive(false);
    30.     //    GoodJob4.SetActive(true);
    31.     //    yield return new WaitForSeconds(7);
    32.     //    GoodJob4.SetActive(false);
    33.     //    GoodJob405.SetActive(true);
    34.     //}
    35.  
    36.     //IEnumerator LoadMenu()
    37.     //{
    38.     //    ADS.SetActive(false);
    39.     //    FinishTutorial.SetActive(true);
    40.     //    yield return new WaitForSeconds(7);
    41.     //    SceneManager.LoadScene("Menu");
    42.     //}
    43. }
    44.  
    Disable:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class LoadNextTutorialFromGoodJob1 : MonoBehaviour
    7. {
    8.     public GameObject WalkLeftBackwardRight;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.  
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.  
    20.     }
    21.  
    22.     public void LoadNextTutorial()
    23.     {
    24.         gameObject.SetActive(false);
    25.         WalkLeftBackwardRight.SetActive(true);
    26.     }
    27. }
    28.  
    Update 2: It disappeared once but then when I test again, it doesn't.
     
    Last edited: Feb 18, 2021
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    I think you want to rename some functions there... you have the same named function in LoadNextTutorial() in both scripts. That's not a problem per se, but that is confusing.

    Beyond that, keep adding Debug.Log() to find out what is happening. In fact, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
    KaiXGT likes this.
  7. KaiXGT

    KaiXGT

    Joined:
    Feb 12, 2021
    Posts:
    13
    I'm sorry, but I just got it working. Thanks for addressing this though!