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 /
  • Help Room /
This question was closed Apr 27, 2018 at 01:48 PM by MajorSmurf for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by MajorSmurf · Apr 26, 2018 at 03:45 PM · unity 5buttonuser interfacecodepagebutton trigger events

Does multiple spamming a button break it?

Hello I am currently working on a system for a unit/object to be created everytime you press a button. Now the unit does create and perform the action that is required of it but sometimes after spam clicking the button and function break which creates an infinite amount of units and I was hoping someone could point something wrong in the code. I am a beginner so you'll have to forgive my poor coding. basically the question is why does my button sometimes break but not other times.


Below is the code: To add to this the button is connected to BuildGoldBot(). If someone could enlighten me that would amazing. Thank you for reading and i hope to discuss solutions and improve myself.


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MineBotCreate : MonoBehaviour {
 
     [SerializeField]
     private GameObject Unit_1;
 
     private GameObject CurrentUnitBuilding;
     private float ConstructionTimer = 0;
     private bool constructing;
     private float buildqueue = 0;
     public Transform SpawnPoint;
 
     // Use this for initialization
     void Start () 
     {
         constructing = false;
     }
 
     // Update is called once per frame
     void Update () 
     {
         if (buildqueue >= 1) 
         {
             constructing = true;
         }
 
         if (buildqueue <= 0) 
         {
             constructing = false;
         }
 
         if (constructing == true) 
         {
             ConstructionTimer += Time.deltaTime;
             if (ConstructionTimer >= 5) 
             {
                 BuildingGoldBot ();
             }
         }
     }
 
     public void BuildGoldBot()
     {
         if (CurrentUnitBuilding == null) 
         {
             Debug.Log("IM WORKING!");
             Gold_Gathering.gold -= 10;
             buildqueue++;
             Debug.Log ("I am spawning this many units " + buildqueue);
         }
     }
 
     public void BuildingGoldBot()
     {
         CurrentUnitBuilding = Instantiate (Unit_1, SpawnPoint.position, SpawnPoint.rotation);
         CurrentUnitBuilding = null;
         ConstructionTimer = 0;
         buildqueue--;
     }
 }



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

  • Sort: 
avatar image
3
Best Answer

Answer by CurtisGM · Apr 26, 2018 at 05:00 PM

Hello @MajorSmurf!

I took it upon myself to start an empty project. I quickly added a button to a canvas, added your script to the Main Camera, assigned an empty prefab to the script and finally hooked up the button to the use the Main Camera's script.


I then went to town on that button to see if I could break it and (unfortunately?) it worked flawlessly. Your script spawned the appropriate amount of units despite my efforts to spam break it.


Might you know of any sure-fire ways to replicate your issue? Maybe the issue lies in a different script that is inherently affecting this one?


On a side note, to my knowledge, buttons within Unity should have no problem handling spam clicking :)

Comment
Add comment · Show 7 · 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 Geejayz · Apr 26, 2018 at 05:10 PM 0
Share

Love this reply "I then went town on that button..." Consider yourself rewarded. :)

avatar image MajorSmurf · Apr 26, 2018 at 09:15 PM 0
Share

thanks for trying @CurtisG$$anonymous$$ i have no sure fire way of replicating this issue unless you want to see the whole project. but for some reason it happens only certain times...... btw as a side note is this code good or is it really bad?

thinking about it may have something to do with the code on the unit it spawns. but outside of sending the whole project to see if you can replicate it.

avatar image CurtisGM MajorSmurf · Apr 26, 2018 at 09:59 PM 0
Share

Hello again @$$anonymous$$ajorSmurf

If you have no issues sending the whole project, then I would be happy to give it a try on my system here to see if I can further assist you with the issue. Otherwise, if you find a way to replicate the issue later on, feel free to let me know too.

As for the quality of the code, different people have different coding styles. In my humble opinion, I believe that readability is one of the most important things to see in code (aside from performance). There are ways to shorten down the code further without sacrificing readability, but overall, it runs smoothly and is easy to read so I feel this is solid code :)

($$anonymous$$udos to you for na$$anonymous$$g your variables something meaningful, I have seen a lot of code snippets from many other programmers, and it's a pain to have to go through code with variables named 'bQ' or 'gG' rather than 'buildQueue' or 'goldGathering')

@Geejayz Hahah, I'm glad you liked the comment :P

avatar image MajorSmurf CurtisGM · Apr 27, 2018 at 12:21 AM 0
Share

@CurtisG$$anonymous$$ Hi i created a public google drive link cause its too big to fit as an attachment and i forgot how to compress zips and basically i got lazy. when you download and open you should see a test level with a bunch of stuff but the issue I've talked about here is there should be a cube called $$anonymous$$ebothome and golddepo with a canvas and a button over it saying $$anonymous$$ebot (or $$anonymous$$ebo cause i forgot to edit the size of the button). which will spawn a bot to go back forth between the home and golddepo. Now on my end the bug appears to come and go randomly and i cant 100% replicate it but i would really like to make sure that it doesn't start doing when i start level designing.

//link removed

If anyone downloads it and can share their thoughts that would great. But thanks for your help Curtis. I'm not gonna lie you'll most likely be seeing alot of me :D

and btw saying i was new was a slight lie i should have said new to unity but ive used unreal and done a little c# coding so yeh i know the importance of na$$anonymous$$g variables... capital letters are still my nightmare. get so confused when ive added a rnadom capital somewhere :L

Show more comments
avatar image
0

Answer by NFad · Apr 27, 2018 at 05:43 AM

If it is really working flawlessly for some people but not for you, how about adding a timer float and an event fire rate float, adding

Time.deltaTime

to your timer and reset it every time (timer > eventFireRate) That might prevent spamming and you can set it really low too.
Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

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

Can't access functions on my UI button 1 Answer

How do I can use Arduino push button trigger unity UI button (on click ()) , no direct active effect? code can function but it had duplicate code and not the result that I want. 0 Answers

How to fix button clones that aren't working? 0 Answers

Disabled UI button and skip over in navigation 0 Answers

unity UI button is not behaving correctly in build 0 Answers


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