Search Unity

interactble buttons based on points

Discussion in '2D' started by Adeogo123, Jul 9, 2019.

  1. Adeogo123

    Adeogo123

    Joined:
    Nov 5, 2018
    Posts:
    4
    so im making a code that allows the player to get points and when the points increases it makes the next button in the array interactable but its not working. Can yu help me please
    This is the code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using TMPro;
    using UnityEngine.UI;

    public class LevelManager : MonoBehaviour
    {

    public Button[] Levels;


    public TextMeshProUGUI currentScoreText;

    int LevelIndex = 0;

    int LevelsCount;

    int currentScore;

    void Start()
    {
    LevelsCount = Levels.Length;
    currentScore = 0;
    }


    void Update()
    {
    if(currentScore > LevelIndex)
    {
    InstantiateLevel();
    }
    }

    public void Checkpoint()
    {
    currentScore ++;
    SetScore();
    }

    public void SetScore()
    {
    currentScoreText.text = currentScore.ToString();
    }

    public void InstantiateLevel()
    {
    LevelIndex = LevelIndex++;
    //Levels[LevelIndex].interactable = (currentScoreText.text == LevelIndex);

    }

    }
    upload_2019-7-9_12-18-48.png
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    80
    Can you explain what exactly isnt working? Where is your Checkpoint() function being called, from an on-click event? I see that it is where you increase currentScore but it isn't being called through the script. If it is being called from a button click, then try adding in some Debug.Logs in each function and see what is happening and what isn't.

    Lastly, please post your code with the "Insert Code" button on the text ribbon.
     
  3. Adeogo123

    Adeogo123

    Joined:
    Nov 5, 2018
    Posts:
    4
    the check point is for something else (so when i complete a level in game it adds a points i will change it to be called by another script) but the problem is that i want it so that as the point increases the next button becomes interactable
     
  4. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    80
    Right, but it looks like currentScore is what drives the levels to be unlocked. So far, the CheckPoint appears to be the only function that increases currentScore. If you are using that during runtime to increase the score, then that makes sense. If not, you need something else to increase the currentScore. From what I have learned, it is easier to drag the button into a public GameObject variable and then disable that. But, here is a video on disabling/enabling the buttons.