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. Dismiss Notice

How to activate gameobject after 0.25 sec?

Discussion in '2D' started by RuneShiStorm, Jan 29, 2021.

  1. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    I have this script in my game that allow players to pick up stuff from lockers...
    Problem is that the item inside the locker activate same time as you open the locker (Probably since its located inside void Update. Is there a way to take this function out of Void Update or delay the activation of the object?

    Code (csharp):
    1.  
    2.  
    3.   void Update()
    4.    {
    5.  
    6.       if (waitForPress && controls.Player.ActionButton.triggered)
    7.             {
    8.                 itemInside.SetActive(true);
    9.                 waitForPress = false;
    10.  
    11.                 if (PlayerPrefs.GetInt(this.name + "itemInside") == 1)
    12.                 {
    13.                     Destroy(itemInside);//Destroying the item
    14.                 }
    15.                 else
    16.  
    17.                 itemInside.SetActive(true);// I want this to be active after 0.25 sec...
    18.  
    19.  
    20.                 openMode = true;
    21.                 lockerAnimator.SetBool("OpenMode", true);
    22.                 saveThis();
    23.             }
    24. }
    25.  
    26.  
    Thanks in advnace!
     
  2. Kalliber95

    Kalliber95

    Joined:
    Mar 27, 2015
    Posts:
    19
    so you need a few things.

    Since you want to check every 0.25 ms

    Code (CSharp):
    1.  
    2.  
    3.     public float timer = 0f;
    4.     bool Active;
    5.  
    6.     void Update()
    7.     {
    8.      
    9.  
    10.             timer += Time.time * 1000f;
    11.        
    12.         if (timer == 0.250f && Active==false)
    13.         {
    14.             Debug.Log(timer);
    15.           // Execute code here itemInside.SetActive(true)
    16.           // Reset timer and change bool to true;
    17.             timer = 0;
    18.             Active = true;
    19.         }
    20.        
    21.     }
    22.  
    the bool Active is just to prevent it being called multiple times when its already been active.

    if you want the timer to only run when Active is false you can just simply add
    Code (CSharp):
    1.   if (Active == false)
    2.         {
    3.             timer += Time.time * 1000f;
    4.         }
    We multiply the Time.time value by 1000 to find the value in milliseconds as 1000 milliseconds are in 1 second.

    Hope this helps and makes some sense.
     
  3. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    Thank you so much!
    yes it make sense! I dont have time to try it out today but it looks like what im looking for!
    Thanks!
     
  4. Dedi6

    Dedi6

    Joined:
    Jan 20, 2019
    Posts:
    119
    You could also use a Coroutine to execute a function after x time.
     
    RuneShiStorm likes this.
  5. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    T
    Thanks.
    I only worry how it will effect my save script cuz its important that the code dont intereupt it somehow since I dont really know how it works :p