Update function not updating like it should be.?
so what I'm trying to do here is to active a shield every time the player touch the screen and after 10 sec player can set it active again,
my problem is that the update function only active it once and after 10 sec I keep clicking like +10 click then it active it again
here is the code
public GameObject shield;
bool canClink= true;
private float timetonextclick;
void Start () {
}
public void Update () {
if (Input.touchCount > 0) {
Touch touch = Input.GetTouch (0);
if (touch.type == TouchType.Direct) {
if ( canClink == true ) {
GameObject myShield = (GameObject)Instantiate (shield, transform.position, Quaternion.identity);
CreateShield shieldScript = myShield.GetComponent<CreateShield> ();
shieldScript.myOwner = this.gameObject;
canClink = false;
timetonextclick = 0.0f;
}
if (canClink == false) {
timetonextclick += Time.deltaTime;
}
if (timetonextclick >=10f) {
canClink = true;
}
}
}
} }
is there something wrong in the code which make this weird behavior?
Answer by Jwizard93 · Jun 22, 2017 at 12:09 AM
Hello. I looked at this for a minute. No promises. But It looks like you only increment the timetonextclick when the touch count > 0 and when TouchType is Direct.
I think you need to restructure this nested IF block. timeToNextClick needs to increment if canClick is false and that's it. Move it out of the other IF statements.
Hope this is helpful.
thanks man, after reading your comment I looked at it again and yes it makes sense it worked fine now.