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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Wait and then.... Script

Discussion in 'Scripting' started by Zero101, Apr 22, 2015.

  1. Zero101

    Zero101

    Joined:
    Apr 18, 2015
    Posts:
    14
    Hi I want to freeze the game but to freeze it I need to click and click is my button to fire and so it summons a "bullet" and then after that freezes the game before the bullet instantiated and so i somehow I need to tell the script to wait a bit so that Unity has enough time to Instantiate the bullet and then after the wait time is over he can destroy the bullet but I don't know how.
    Code (CSharp):
    1.         else {
    2.             GameObject.Find("Live1Icon").GetComponent<SpriteRenderer>().enabled = false;
    3.             GameObject.Find("Live2Icon").GetComponent<SpriteRenderer>().enabled = false;
    4.             GameObject.Find("Live3Icon").GetComponent<SpriteRenderer>().enabled = false;
    5.             GameObject.Find("Main_Player_LVL1").GetComponent<SpriteRenderer>().enabled = false;
    6.             //wait 1/4 second and do the following:
    7.             Destroy (GameObject.Find ("Player_Shot_LVL1"));
    8.         }
    9.     }
     
  2. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    shouldnt the bullet be in charge of destroying its self, and to do so you could make a simpler timer into the bullets Update method, using Time.time and Time.deltaTime.

    They bullet its self, should really be incharged if its own movmeant and be killing its self.
     
  3. Zero101

    Zero101

    Joined:
    Apr 18, 2015
    Posts:
    14
    I wil try doeing so, thanks for the suggestion
     
  4. Zero101

    Zero101

    Joined:
    Apr 18, 2015
    Posts:
    14
    Thanks that solved my problem, I did not think that will worked but it did thank you!
     
  5. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    ya a simple script like this should do the job.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AutoDes : MonoBehaviour
    5. {
    6.     [SerializeField]
    7.     private float lifeTime = 3f;
    8.     private float curTime;
    9.  
    10.     private void Start()
    11.     {
    12.         curTime = 0f;
    13.     }
    14.  
    15.     private void Update()
    16.     {
    17.         curTime += Time.deltaTime;
    18.         if (curTime >= lifeTime)
    19.             Destroy(gameObject);
    20.     }
    21. }
    22.  
    lifeTime is how long you want the bullet to live in seconds.
     
    Last edited: Apr 22, 2015