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 !