DEV Community

Cover image for UdonSharp - Easy Script for Time Based Operations
Chris
Chris

Posted on

UdonSharp - Easy Script for Time Based Operations


script

using UdonSharp;
using UnityEngine;

public class TestTimer : UdonSharpBehaviour
{
    // In this example I just have this hooked up to a light source to make it a slow blinking light
    [SerializeField] private GameObject _lightBulb;

    // A float to enter the desired time
    [SerializeField] private float _delayInSeconds = 10.0f; 

    // I serialized this one just so I can watch it in the editor
    [SerializeField] private float _elapsedTime = 0.0f; 


    private void Update()
    {
        // Time.deltaTime is just x seconds from previous frame
        // so we increment the elapsed timer until it's equal to or passed the desired time
        _elapsedTime += Time.deltaTime; 

        if(_elapsedTime >= _delayInSeconds)
        {
            // Perform whatever operation you want here
            _lightBulb.SetActive(!_lightBulb.activeSelf); 

            // Reset timer and start over
            _elapsedTime = 0.0f; 
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

here it is in action

Image description


description

As you may know, UdonSharp is a programming language built for VRChat that has basic C#/Unity library support, unfortunately there's a lot more of those libraries that aren't available.

When I did a quick search, all the suggested methods for doing timer based events appeared a little more complex to follow then seemed necessary, which is why I wanted to post this simple script in case anyone is just looking for something easy.

Thanks for reading!

Top comments (0)