C
C#15mo ago
dc17.

How to wait 2 seconds before activating a part of code?

my goal is to make the
rb.isKinematic = false
rb.isKinematic = false
work after 2 seconds. here is my code;
using UnityEngine;

public class GravityOn : MonoBehaviour
{
private Rigidbody rb;

void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
// Code to trigger when the collision occurs
print("Gravity");

rb = GetComponent<Rigidbody>();
rb.isKinematic = false;
}
}
}
using UnityEngine;

public class GravityOn : MonoBehaviour
{
private Rigidbody rb;

void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
// Code to trigger when the collision occurs
print("Gravity");

rb = GetComponent<Rigidbody>();
rb.isKinematic = false;
}
}
}
thank you! 🙂
16 Replies
Buddy
Buddy15mo ago
Wait, why are you getting the rigidbody with each collision?
Angius
Angius15mo ago
In normal C# you'd use await Task.Delay(2000)
Buddy
Buddy15mo ago
Just save the Rigidbody component in a variable and use that
Angius
Angius15mo ago
Unity probably has its own weird thing
Buddy
Buddy15mo ago
Unity has Coroutines https://docs.unity3d.com/Manual/Coroutines.html
By default, Unity resumes a coroutine on the frame after a yield statement. If you want to introduce a time delay, use WaitForSeconds:
dc17.
dc17.15mo ago
to be honest im not sure. but will do that
Buddy
Buddy15mo ago
GetComponent is expensive So avoid calling it more than once if not required
dc17.
dc17.15mo ago
well technically it will be called once so will it matter in this case? but yeah im getting rid of it
Buddy
Buddy15mo ago
It's supposed to be called once, but what if you enter then exit and repeat? You will be calling GetComponent more than once, which isn't needed. Now it doesn't affect the performance that much, but it does help a little to cache it in a variable, depending on how frequent the calls are. If you were calling GetComponent in Update, now that would be a fairly big impact.
dc17.
dc17.15mo ago
yeah i get what you mean and it makes sense. but in this case when the player enters the object should fall down and be destroyed but it will definetly be important on other things
Buddy
Buddy15mo ago
Then yeah, it shouldn't make a big difference, except you use GetComponent when the component is loaded and not when the events happen. Both being called once, so you'll be fine.
dc17.
dc17.15mo ago
also on this link, im quiet confused on how i have to use it. i believe i have to be using
WaitForSeconds
WaitForSeconds
right?
Buddy
Buddy15mo ago
Correct
dc17.
dc17.15mo ago
how may i apply this to my code?
Buddy
Buddy15mo ago
Check the docs and scroll down until you see Coroutine time delay It shows an example of how it applies Tasks are currently not recommended for Unity since it still runs even if you change the scene. So unexpected things might / will happen.
dc17.
dc17.15mo ago
i think i managed to do it, thank you so much ehh it didnt work im trying to find th issue here yep it works now, thank you again. do i have to close this post now?