How to use if statement in start function

hello… im trying to create a timebomb game…
Bomb script attach on Bomb prefab , whenever player drop a bomb its explode after 3 sec… at this point all are ok. I don’t know how to use if statement when ever time button is active bomb stop explode if button pressed bomb blast immediately.

plz check the bomb script.

void Start()
    {
		
		Invoke("Explode", 3f);

		Start Coroutine(Collider());
	}

im using button on player script to control explosion but its explosion in 3 sec

   public void TimeBomb()
    {
        TimeBombButton.gameObject.SetActive(true);
        bomb.enabled = false;
        Debug.Log(" Bomb False");
        
    }

   

   
    public void ButtonPress()
    {
        
        Bomb.enabled = true;
        Debug.Log(" Bomb Enable");
        StartCoroutine(Timer());
        GameObject.Find("Bomb(Clone)").GetComponent<Bomb>().enabled = true;
        // 3
        bomb.explosion();
        //bomb.GetComponent<MeshRenderer>().enabled = true;
    }

    private IEnumerator Timer()
    {
        yield return new WaitForSeconds(0.1f);
        bomb.GetComponent<MeshRenderer>().enabled = true;
        Bomb.enabled = false;
    }


You could add this to the Bomb script.

public float Countdown = 3.0f; // Set to 0 while Fuse is running to immediately explode

public IEnumerator Fuse(float fuseTime = 3.0f)
{
    Countdown = fuseTime;

    while (Countdown > 0.0f)
    {
        Countdown -= Time.deltaTime;
        yield return null; // Wait for the next frame
    }
    
    Explode();
}

In Start(), replace Invoke("Explode", 3f); with StartCoroutine(Fuse(3f));
In ButtonPress() you can make the bomb explode by setting bomb.Countdown = 0f; or with StartCoroutine(bomb.Fuse(0f));

1 Like

Thanks for replay… its not working bcz when i command bomb.countdown = 0f , bomb time change to 0 in prefab but not explode. so i use this code for instant explosion
GameObject.Find(“Bomb(Clone)”).GetComponent().Countdown = 0f; bcz when I drop a bomb, bomb prefab name show Bomb(Clone) on hierarchy.
when button is setactive then
GameObject.Find(“Bomb(Clone)”).GetComponent().Countdown = 1000f;
when button press countdown to 0…

but another prob is found whenever use 2 bomb 1st bomb countdown time change to 1000 but 2nd bomb time not chnage like 3f… may be i use StartCoroutine(Fuse(3f)); in start function when i use its default time set 3f.