Problem is not reproducible or outdated
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--;
}
}
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 :)
Love this reply "I then went town on that button..." Consider yourself rewarded. :)
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.
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
@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
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
to your timer and reset it every time (timer > eventFireRate) That might prevent spamming and you can set it really low too.Time.deltaTime