- Home /
activating and deactivating button with if-then statement
So I made the roll-a-ball game with the unity tut. and I made some modifications such as more levels and a mainmenu and other stuff. After a roll-a-ball game has been completed(if you have seen the tut. you will understand better) by picking up all of the little cubes(In my case 18) with the ball, the words "you win" or (something along those lines) will pop up. Now scince I have multiple levels I decided to put each level on it's own seperate scene. I also decided to go between those scenes by using buttons. I made a script to deactivate a button when you first start that level and also to activate it when your "count" has reached 18. I know that my script is working(somewhat) because the button is deactivated when I start my "level". My problem is that it won't reactivate when my count reaches 18. Is this because my Button won't be visible when you move the camera or is it because of my script?? No matter the reason, can somebody help me with this? Please feel free to ask questions, I will try to refresh this page alot and read your responses! This question is on the same project as the one we had a different problem on(if that makes sense) so I will add your names in here to see if you can help me again... @Brogan89 @TheDJBuntin
(The Name of the script is ContinueBtnController and the name of the button is continueBtn) so you don't get confused.
Here is my !!!C# script!!! :
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class ContinueBtnController : MonoBehaviour {
public GameObject continueBtn;
private Text countText;
private int count;
private Text winText;
void Start()
{
{
count = 0;
}
}
**void FixedUpdate()
{
if (count >= 18)
{
continueBtn.SetActive(true);
}
else
{
continueBtn.SetActive(false)
}
}**
void SetCountText()
{
countText.text = "Count: " + count.ToString();
if (count >= 18)
winText.text = "You've won!";
}
}
So everything but the if then Statement(stuff that has double * around it is part of the if then statement) is just there so the if then statement can reference the things I wrote in it.
Here is the beginning of a game with the button and "you win" because I haven't reached 18 yet or started.
I do also have a main script that actually makes the other buttons, moving the player, and texts, work, I just started a new script so I wouldn't have to deal with all of that when I was just using it for a button.
Answer by Raimi · Jul 09, 2017 at 05:58 PM
I can't see where your count is being updated. There should be somewhere count += 1?
So the main Script for this game is the "Player controller" script and that is where the "count" is actually being updated. I just have the extra stuff in there so the if then statement can reference it's self.
Well, the above code should work if you increment the count. at the $$anonymous$$ute, the above code(first code) doesn't increment the count, so it is always 0. Count is set to private so needs to be incremented within this class or have some kind of setter
Also you have too many curly braces in the start method
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ContinueBtnController : $$anonymous$$onoBehaviour
{
public GameObject continueBtn;
private Text countText;
private int count;
private Text winText;
void Start()
{
count = 0;
}
void FixedUpdate()
{
if (count >= 18)
{
continueBtn.SetActive(true);
}
else
{
continueBtn.SetActive(false);
}
}
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!";
}
}
So there is my code now and it still won't work.....
The "count" was being shown and kept track of in the lefthand corner, but that was the main script and and not this one... So I'm still lost on why it doesn't work...
Answer by Jwizard93 · Jul 09, 2017 at 10:53 PM
The script is on the button right? So do this:
Make the "count" variable in the MainScript to be public.
Now in the button script declare a reference to the type MainScript;
public MainScript mainScriptRef;
Now in the inspector for the Button drag the object that has the MainScript into the field you created for the button.
The button Code should now read:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ContinueBtnController : MonoBehaviour
{
public GameObject continueBtn;
public MainScript mainScriptReference;
private Text countText;
private Text winText;
void FixedUpdate()
{
if (mainScriptReference.count >= 18)
{
continueBtn.SetActive(true);
}
else
{
continueBtn.SetActive(false);
}
}
void SetCountText()
{
countText.text = "Count: " + mainScriptReference.count.ToString();
if (mainScriptReference.count >= 18)
winText.text = "You've won!";
}
}
I believe your main problem is that you aren't realizing that count in one script is not the same variable as count in another script. ANd then you tried to fix it by having this script also "pick up" objects but your buttonController is not involved in the collisions so nothing is going to happen.
Ok, Ya that help alot. I didn't think about that part enough... Thanks alot! BUT.............Lmao. It still didn't work... I'll attach some screen shot so maybe you can give me some more insight...
so in this pic in the bottom right what should I put for the empty box that has the "PlayerControllerReference"?? (Btw PlayerController is my main script)
I just remembered, I don't think my if-then statement is working properly anymore because it is still active when I start a game, and before it would not be active at the beginning of a game.
So for this one whenever I try to test my game it pops up all of these errors. When I double click on them it takes me to this script and highlights this(see pic)
And my last additional problem was that Should I attach all of this to the button? because if I deactivate it, then it won't reactivate because its deactivated.. I can attach it to a constant active like my "player"? In this pic "player is highlighted and in the inspector you can see that I have done the same thing for other texts.
Oops forgot to post your name... @Jwizard93
Ok so I added the PlayerController script to the empty box and now it deactivates but it still doesnt reactivate once it hits 18 items... so I'm even more confused... @jwizard93
Your answer
Follow this Question
Related Questions
adding delay to certain part of script.,Adding Delay to part of a script? 2 Answers
Animation doesnt play ?? 0 Answers
something is wrong with my code cant find out...i am a newbie 1 Answer
help with 3d scroller skateboard chase script 0 Answers
Armature bone unable to rotate while animation playing 1 Answer