How do I do if statements with a button

So I wanted to add on to the roll-a-ball tutorial by adding some more complex levels to it. I am using buttons to go between the different scenes that have the different levels on them. What I have setup so far is to have my if statement refer to how many items have been collected (in this case two for the sake of time) to determine if I have won and to therefore pop up the button to change scenes. I am using “continueBtn.SetActive(true)/(false);” to deactivate and activate the button therefore “popping” it up. (refer to my script that I will be posting below if you are confused) My problem is that It won’t be deactivated when I don’t have those two items collected and it won’t be active when I do have them. I have tried to replace the false with true and I have tried to delete the “else” portion of the statement and nothing truly works. I know my problem isn’t that the script isn’t attached to the button itself because I have checked that many times and with some of my experiments of deletion and replacement It sometimes is popped up before I even get those two items. (to understand what I have tried and and what I haven’t look at my short vid (I will post a link below). THANKS FOR TAKING YOUR TIME TO HELP ME!! :slight_smile:

Here is my short vid (I highly recommend you to watch it) : - YouTube

Here is my script:

public class ContinueBtnController : MonoBehaviour
{

public GameObject continueBtn;
public PlayerController PlayerControllerReference;
 
private Text countText;
private int count;
private Text winText;


void Start()
{ 
        count = 0;
    continueBtn.SetActive(false);
}

void FixedUpdate()
{
    if (PlayerControllerReference.count >= 2)
    {
        continueBtn.SetActive(true);
    }
    else
    {
        continueBtn.SetActive(false);
    }
}

void SetCountText()
{
    countText.text = "Count: " + PlayerControllerReference.count.ToString();
    if (PlayerControllerReference.count >= 18)
        winText.text = "You've won!";
}

}

Look this count number does not incrementing when you collect those things.

106123-asd.png

but this text do :

106124-asdasd.png

probably you are reseting that count elsewhere, probably in PlayerCotroller script.
please chek there, if you can not find your solution, just add your other scripts here too for us to look for where is the problem.

if this answer helped, please consider marking as correct. this helps the community.

Oh, wow didnt even see that. Thankyou, it looks like that is my issue so here is my only other script that has something in it that refers to the “count” and I looked through it again to see where the count would be constantly reset but I couldn’t find it. Maybe you guys could. Thanks again for your time!! :slight_smile:
@NorthStar79 @EdwinChua
Player controller Script:

public float speed;
public float time = 0;
public Text countText;
public Text winText;
public int count;

private Rigidbody rb;
 

void Start()
{
    {
        rb = GetComponent<Rigidbody>();
        count = 0;
        SetCountText();
        winText.text = "";
    }
}

void FixedUpdate()
{
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

        rb.AddForce(movement * speed);
    }
}

void OnTriggerEnter(Collider other)
{

    if (other.gameObject.CompareTag("Pick Up"))
    {
        other.gameObject.SetActive(false);
        count = count + 1;
        SetCountText();
    }
}

void SetCountText()
{
    countText.text = "Count: " + count.ToString();
    if (count >= 18)
        winText.text = "You've won!";
} 

}

That is a good idea. Ok, so I got rid of the game btn controller script and put it all on the player controller script and it still wont work. In addition to that the count won’t go up as I collect the items but I’m starting to think you won’t see it go up because the “you win” text only pops up when the 18 items have been collected as well so I think we’re back to square one on why this won’t work. However I have found another lead (I think). It says in my console that “continue btn hasn’t been assigned”. But this doesn’t make sense either, I have checked everywhere and literally everything possible I have added continue btn to the script in the inspector or vice versa. Sorry that this has been such a hassle and again thanks for your time and help!
@NorthStar79 @EdwinChua