Weird timer issue.

Hi guys, I’ve got a problem I just can’t figure out.
I’ve got a pretty standard (i think) timer system, i can see in the inspector that my timerShot does reset when I don’t call anything in my “if loop”, and soon as I try to actually do something, the shotTimer DOESN’t reset !

the code with nothing but debug : (both shotTimer and shotDelay are public float)
if ( shotTimer >= shotDelay ){
Debug.Log (“TIIIIIIIIIIIIIIR !”);
//var clone = Instantiate (lanceprefab, transform.position, Quaternion.identity) asGameObject;
//clone.transform.localScale = transform.localScale;
shotTimer=0;
} as expected, as soon as shotTimer reaches shotDelay, shotTimer gets back to 0.

Now if i try to instantiate my object, or call a dedicated public void to do it, the timer just runs infinite and i’ve got an infinity of instantiates…

This is driving me crazy, any help would be much appreciated !!!

Code tags?

Not sure what you are asking, this is C#, if this is your question !
Sorry, I’m kinda new to scripting stuff… :blush:

To make things a bit easier: Using code tags properly - Unity Engine - Unity Discussions

1 Like
publicvoidRangeAttack() {
Debug.Log ("loiiiiiiiiiiiiiiiiiin !");
shotTimer += Time.deltaTime ;

if ( shotTimer >= shotDelay ){
Debug.Log ("TIIIIIIIIIIIIIIR !");
varclone = Instantiate (lanceprefab, transform.position, Quaternion.identity) asGameObject;
clone.transform.localScale = transform.localScale;
shotTimer=0;
}

}

in this form, I get infinite instantiates and shot timer goes beyond shotDelay. if I //out the cloning stuff, my shotTimer reset properly.
-_-

NB : RangeAttack is called from another “if” that seems ok as it set my booleans properly in the inspector and trigger shotimer and shotdelay properly when I //out the instantiates stuff.
This other “if” is called by raycasting method.

Any scripts on the lanceprefab? Or is there any other script in the scene that could possibly manipulate the shotTimer value?

if( shotTimer >= shotDelay)

this conditions is always true you say? Can you debug what the actual values in here are, when your code starts looping

here is a capture of the screen :
once shotTimer gets over ShotDelay ( value start from zero ) it continues increasing infintly and infinity of lanceprefab are created

my conditions are : if player is hit on raycasting (from sightstart to sightend). the condition seems to be ok : when player is not here, shottimer stop running, and nothing instantiates…

( and no other script on prefab, yes on the Guard and the Player though, but I don’t see why the simple fact of taking out the instantiate command of the loop would make the “shotTimer=0;” work, I’m so lost here… )

What do these conditions have to do with the code you provided? This doesn’t make sense.
There has to be something that either spawns the objects or manipulates the variable, otherwise i see no logical reason why this should behave as you said.
Once again, what is attached to the lanceprefabs? And where do you call that method?

I provided just the part that I thought relevant. There is Nothing attached to lanceprefab. Just a renderer, ( I guess I’ll have to attach a rigidbody2D and a collider to move it and collide with player, but this is not my problem right now).

Here is the whole code, this is my prototype AI for this type of enemy. the //out parts are mostly for testing purposes.

usingUnityEngine;
usingSystem.Collections;

publicclassGuardIA : AbstractBehavior {

publicTransformsightStart, sightEnd;
publicTransformbackStart, backEnd;
publicTransformcloseEnd, closeStart;
publicboolspottedClose;
publicboolspottedFront;
publicboolspottedBack;
publicfloatGuardSpeed;
publicboolaDroite;
publicLayerMaskPlayer;
//Walkscript;
privateAnimatoranimator;
publicfloatshotDelay;
publicTransformlanceprefab;
publicfloatlanceSpeed;
publicfloatshotTimer;


//Usethisforinitialization

voidStart()
{
animator = GetComponent<Animator> ();
//script = GameObject.Find("Player").GetComponent<Walk>(); // sionveutajouterlefaitqueguardentenduniquementsiplayercourt, ajoutercetteconditionenbasdansbehaviours
spottedFront = false;
spottedBack = false;
shotTimer = 0;
animator.SetInteger ("AnimState", 0);

}

//Updateiscalledonceperframe
voidUpdate () {

aDroite = sightStart.position.x > sightEnd.position.x;
Raycasting ();
//Behaviours ();


}

voidRaycasting()
{
Debug.DrawLine(backEnd.position, backStart.position, Color.green);
Debug.DrawLine(sightEnd.position, sightStart.position, Color.green);
spottedFront = Physics2D.Linecast(sightEnd.position, sightStart.position, 1 << LayerMask.NameToLayer("Player"));
//spottedClose = Physics2D.Linecast(closeEnd.position, closeStart.position, 1 << LayerMask.NameToLayer("Player")); Dealwiththatbehaviourfrom "coneSightscript ?"
spottedBack = Physics2D.Linecast(backEnd.position, backStart.position, 1 << LayerMask.NameToLayer("Player"));

if (!spottedFront && !spottedBack) { //attentepardéfaut, immobile
animator.SetInteger ("AnimState", 0);
}
if (spottedClose == true && aDroite) { //s'approcheàdroiteenfrappantavecarmemêlée

animator.SetInteger ("AnimState", 1);
transform.localScale = newVector3 (1f, 1f, 1f);
GetComponent<Rigidbody2D> ().velocity = newVector2 (-GuardSpeed, GetComponent<Rigidbody2D> ().velocity.y);
CloseAttack();
} else {

if (spottedClose == true) { //s'approcheàgaucheenattaquemelée
animator.SetInteger ("AnimState", 1);
transform.localScale = newVector3 (-1f, 1f, 1f);
GetComponent<Rigidbody2D> ().velocity = newVector2 (GuardSpeed, GetComponent<Rigidbody2D> ().velocity.y);
CloseAttack();
}
}

if (spottedBack == true && aDroite ) { //seretourne

animator.SetInteger ("AnimState", 1);
transform.localScale = newVector3 (-1f, 1f, 1f);
//GetComponent<Rigidbody2D> ().velocity = newVector2 (GuardSpeed, GetComponent<Rigidbody2D> ().velocity.y);
} else {

if (spottedBack == true && !aDroite) { //seretourne
animator.SetInteger ("AnimState", 1);
transform.localScale = newVector3 (1f, 1f, 1f);
//GetComponent<Rigidbody2D> ().velocity = newVector2 (-GuardSpeed, GetComponent<Rigidbody2D> ().velocity.y);
}


}

if (spottedFront && !spottedClose) {
//animator.SetInteger("AnimState", 2); TODOanim
RangeAttack();

}

}


publicvoidCloseAttack () {

Debug.Log ("Proooooooche !");
}

publicvoidRangeAttack() {
Debug.Log ("loiiiiiiiiiiiiiiiiiin !");
shotTimer += Time.deltaTime ;

//Vector2direction = body2d.transform.position - transform.position;
//direction.Normalize ();
if ( shotTimer >= shotDelay ){
Debug.Log ("TIIIIIIIIIIIIIIR !");
varclone = Instantiate (lanceprefab, transform.position, Quaternion.identity) asGameObject;
clone.transform.localScale = transform.localScale;
shotTimer=0;
}

}

//publicvoidCreateProjectile(Vector2pos){
//varclone = Instantiate (lanceprefab, pos, Quaternion.identity) asGameObject;
//clone.transform.localScale = transform.localScale;
}

as you can see, raycast is checking bool conditions, and then call my “range attack script” ^^