DEV Community

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

Posted on

2

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!

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay