Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

need a second delay in script

Discussion in 'Scripting' started by MrThunderboi, Aug 28, 2022.

  1. MrThunderboi

    MrThunderboi

    Joined:
    Aug 26, 2022
    Posts:
    14
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Bomb : MonoBehaviour
    {
    public CameraShake cameraShake;
    public ParticleSystem boom;
    void Start()
    {

    }


    void Update()
    {
    if (Input.GetMouseButtonDown(0))
    {
    yield return new WaitForSeconds(1);
    StartCoroutine(cameraShake.Shake(.15f, .4f));
    boom.Play();
    //wait for a second
    Destroy(gameObject);
    }

    }

    }
    Where it says wait for a second i dont know how please help!
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,455
    Google how to program a timer (with bool to check if it already ran once)
    Or use a coroutine
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
  4. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    Destroy has an override, you can pass a float to specify after how many seconds the object should be destroyed.

    See here.
     
    Kurt-Dekker likes this.
  5. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    316
    You can either put it at the end of the coroutine or use:
    yield return StartCoroutine(cameraShake....)
    and in your coroutine you add:
    yield return new WaitForSeconds(1f);

    And for the Destroy:
    like Zalosath says, use the delayed method or if you want to do more than just destroy, you can use:
    Invoke("Cleanup", 1f);
    which will call Cleanup() which you will need to write where you can use Destory(gameobject) and other stuff.
     
  6. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    316
    Nice idea using gameobjects to live through scene changes.

    An alternative might be a single gameobject with a TaskManager class that has a List of DelayedTask objects you can add like: TaskManager.Instance.AddDelayedTaks(new DelayedTask {timer=2f, action=()=>DoSomething)
    And then check all tasks and execute them when they are due (can be optimized to not check every frame of course).
     
  7. MrThunderboi

    MrThunderboi

    Joined:
    Aug 26, 2022
    Posts:
    14
    so I just type Cleanup(); ?
     
  8. MrThunderboi

    MrThunderboi

    Joined:
    Aug 26, 2022
    Posts:
    14
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Bomb : MonoBehaviour
    {
    public CameraShake cameraShake;
    public ParticleSystem boom;
    void Start()
    {
    StartCoroutine(Exploded());
    }


    void Update()
    {

    IEnumerator Exploded()
    {
    if(Input.GetMouseButtonDown(0))
    {
    yield return StartCoroutine(cameraShake.Shake(.15f, .4f));
    boom.Play();
    yield return new WaitForSeconds(1f);
    Debug.Log("this will be a destroy");
    }
    }



    }


    }
    My script right now but it says Exploaded is not part of the context also the camera shake is a coroutine in a different script that i just put in here
     
    Olipool likes this.
  9. MrThunderboi

    MrThunderboi

    Joined:
    Aug 26, 2022
    Posts:
    14
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Bomb : MonoBehaviour
    {
    public CameraShake cameraShake;
    public ParticleSystem boom;

    void Update()
    {
    if(Input.GetMouseButtonDown(0))
    {
    StartCoroutine(cameraShake.Shake(.15f, .4f));
    boom.Play();
    Destroy(gameObject, 0.2f);
    }
    }
    }
    works now but laggy for some reason