how to do a if statement to activate and deactivate 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!! :)
Here is my short vid (I highly recommend you to watch it) : https://youtu.be/tryPRDXJH8k
You guys helped me last time I had a problem so I'm going to put your name here @NorthStar79 @EdwinChua (everybody ignored my last question once somebody typed one response but when I replied with their solution not working and put @their name they still didn't respond. So thats why im reposting this question).
Here is my script (the if then statement is at the bottom):
public class PlayerController : MonoBehaviour {
public float speed;
public float time = 0;
public Text countText;
public Text winText;
public int count;
public GameObject ContinueBtn;
private Rigidbody rb;
void Start()
{
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
winText.text = "";
ContinueBtn.SetActive(false);
}
}
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!";
if (count >= 2)
{
ContinueBtn.SetActive(true);
}
else ContinueBtn.SetActive(false);
}
Hello and good morning. I am sorry to hear that you still couldn't solve your problem. as I said on Friday I couldn't response your comments because of weekends. if you attach your project file as Zip. I wish to take a look at it because we can not solve this with just you have provided us yet. normally we shouldn't ask project files but, as this is just a tutorial project, I guess it will not cause any harm.
Don't worry I got impatient and forgot you wouldn't be checking anything this past weekend. Sorry but I don't know how to make a zip file of my project and I don't know how to attach it to this comment. I don't see any options of how to add actual files. @NorthStar79
this section has a file size limit, so you can send a link via dropbox, Drive etc.
for getting your project file, just right-click any asset you have > show in explorer - then go to root directory you will see your project file. it should look like this :
Answer by NorthStar79 · Dec 08, 2017 at 08:05 AM
Hello , sorry for late response, i totally forgot your question :(
BTW fixed your continue button problem but i must say that there are a lot of other errors, but i believe you can fix them yourself.
here steps for how to fix it. first : you need to write correct next lvl name into continue button
and then you need to assign button in player script too
thats all , also here a video how it works:
if this answer helped please consider marking as correct, this way anyone else who looking for the same question can easily find this answer. This is essential for this community.
O$$anonymous$$G how did you get that to work?? I did what you said at least I think so here is a vid to show what I changed but i'm still confused on why it won't work for me. I am however very happy that you got it to work because that means I should. thanks again for your time, I know this is probably getting annoying for you but it is all much appreciated. https://youtu.be/dB5kq$$anonymous$$t$$anonymous$$ouc @NorthStar79
sorry my connection is too bad right now, i am at my annual vacation i couldn't upload picture, (it took about 1 hour to watch your video because connection) you should not attach player controller to your button. if this not solve your problem i will upload fixed version of project when i get s good connection(probably monday)
It didn't fix my problem. I can definitely wait if you just give me instructions on how to use the project file that you give me on monday. I can tell you enough how thankful I am for what you are doing for me. @NorthStar79
Your answer
Follow this Question
Related Questions
Is there a way to detect if a button was pressed? 1 Answer
How to keep bool true even when other method try to set it off 0 Answers
If & Else If with == and && not working 0 Answers
Other way to write a code 0 Answers
SetActive not reacting on Buttonclick 0 Answers