Malakiof
Malakiof

Reputation: 108

bool variable don't change in unity 3d

I'm trying to make a bomberman like game in unity and learning c# in the same time.

I try to change the bool value of targetDestroyed1 var in my code but it seems that there is a problem..

The issue: in the GetNearestTaggedObject1 when a object is founded, the targetDestroyed1 becomes true. but in the if (!targetDestroyed1) condition of InstantiateFire(), targetDestroyed1 always stay false and the instantiate continue instead of stop

Here is my code:

private bool targetDestroyed1;

  void  Start (){
OriginXplosion = new Vector3(transform.position.x, transform.position.y, transform.position.z); 
InstantiateFire();
}

void  Update (){ 
if (target1 != null) {
        Destroy (target1);
    }

public void  InstantiateFire (){
    FireRate = player_actions.FireRate;
    FireRate -= 1;
    targetDestroyed1 = false;

    for(int i1= 0; i1 <= FireRate; i1++){
            float i1_axeZ= transform.position.z + i1;
        if(i1_axeZ <= 7)
        {
            if (!targetDestroyed1)
            {
                Instantiate (ParticulesFeu, Axe1[i1].transform.position, ParticulesFeu.transform.rotation);
                ScanForTarget1();
            }
        }
}
Destroy (gameObject);
}

void  ScanForTarget1 (){
    target1 = GetNearestTaggedObject1();
}


GameObject GetNearestTaggedObject1 (){

    float nearestDistanceSqr= 0.2f;
    GameObject[] taggedGameObjects= GameObject.FindGameObjectsWithTag(searchTag); 

    foreach(GameObject obj in taggedGameObjects) {

        Vector3 objectPos= obj.transform.position;
        float distanceSqr= (objectPos - transform.position).sqrMagnitude;

        if (distanceSqr < nearestDistanceSqr) {
            target1 = obj;
            targetDestroyed1 = true;
            nearestDistanceSqr = distanceSqr;
        }
    }

    return target1;
}

}

Thank you for your help !

Upvotes: 0

Views: 3116

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500055

Look at your InstantiateFire method:

public void  InstantiateFire (){
    FireRate = player_actions.FireRate;
    FireRate -= 1;
    targetDestroyed1 = false;

    ...

You're explicitly setting targetDestroyed1 to false on the third line. So it will always be false on the first iteration of the loop. That means you may call Instantiate and ScanForTarget1, depending on the values of i1_axeZ and other variables.

If the variable is set within GetNearestTaggedObject1, it really will stay set to true for the next iteration of the loop in InstantiateFire - but you'd need to get into the if block again before it had any effect. You should try to step through in the debugger or add more logging to see what's happening. Fundamentally, your code is pretty confusing at the moment, I'm afraid.

Aside from anything else, I'd strongly advise you to create an appropriate class which encapsulates both the target and whether it's destroyed or not... then create a list of that type:

private readonly List<Target> targets = new List<Target>();

That way you don't have to have eight separate variables, with different code manipulating each variable.

Upvotes: 1

Related Questions