C
C#•2y 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
Buddy•2y ago
Wait, why are you getting the rigidbody with each collision?
Angius
Angius•2y ago
In normal C# you'd use await Task.Delay(2000)
Buddy
Buddy•2y ago
Just save the Rigidbody component in a variable and use that
Angius
Angius•2y ago
Unity probably has its own weird thing
Buddy
Buddy•2y 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.OP•2y ago
to be honest im not sure. but will do that
Buddy
Buddy•2y ago
GetComponent is expensive So avoid calling it more than once if not required
dc17.
dc17.OP•2y ago
well technically it will be called once so will it matter in this case? but yeah im getting rid of it
Buddy
Buddy•2y 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.OP•2y 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
Buddy•2y 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.OP•2y 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
Buddy•2y ago
Correct
dc17.
dc17.OP•2y ago
how may i apply this to my code?
Buddy
Buddy•2y 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.OP•2y 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?
Want results from more Discord servers?
Add your server