Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AndrewLackey_97 · Jul 09, 2017 at 04:53 PM · scripting problemgui-button

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.

alt text

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.

alt text

unityproblem3.png (301.9 kB)
unityproblem4.png (288.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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?

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AndrewLackey_97 · Jul 09, 2017 at 06:25 PM 0
Share

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.

avatar image AndrewLackey_97 · Jul 09, 2017 at 06:26 PM 0
Share

alt text

unityproblem5.png (83.0 kB)
avatar image Raimi AndrewLackey_97 · Jul 09, 2017 at 07:29 PM 0
Share

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

avatar image Raimi AndrewLackey_97 · Jul 09, 2017 at 07:33 PM 0
Share

Also you have too many curly braces in the start method

avatar image AndrewLackey_97 Raimi · Jul 09, 2017 at 09:07 PM 0
Share
 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.....

alt text

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...

unityproblem6.png (268.1 kB)
Show more comments
avatar image
0

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AndrewLackey_97 · Jul 10, 2017 at 12:05 AM 0
Share

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...

alt text

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)

unityproblem8.png (361.3 kB)
avatar image AndrewLackey_97 AndrewLackey_97 · Jul 10, 2017 at 12:17 AM 0
Share

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.alt text

unityproblem12.png (67.8 kB)
avatar image AndrewLackey_97 · Jul 10, 2017 at 12:08 AM 0
Share

alt text

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)

alt text

unityproblem9.png (61.1 kB)
unityproblem10.png (50.9 kB)
avatar image AndrewLackey_97 AndrewLackey_97 · Jul 10, 2017 at 12:13 AM 0
Share

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.

alt text

unityproblem11.png (370.6 kB)
avatar image AndrewLackey_97 AndrewLackey_97 · Jul 10, 2017 at 12:14 AM 0
Share

Oops forgot to post your name... @Jwizard93

avatar image AndrewLackey_97 · Jul 10, 2017 at 02:33 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

108 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges