Checking Object Validity and Shuffling an Array

Explanation:

  1. Utilities.IsValid(targetObject) – Checks if targetObject is valid and not null/destroyed.

  2. Utilities.ShuffleArray(numbers) – Randomly shuffles the numbers array.

  3. Calling Interact() – Clicking the object will:

    • Validate if targetObject still exists.

    • Shuffle the numbers array.

    • Log the results to the console.

This script can be attached to an interactable object in VRChat to test object validation and array shuffling.

using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon;

public class UtilitiesExample : UdonSharpBehaviour
{
    public GameObject targetObject; // Assign a GameObject in the inspector
    public int[] numbers = { 1, 2, 3, 4, 5 };

    public override void Interact()
    {
        // Check if the target object is valid
        if (Utilities.IsValid(targetObject))
        {
            Debug.Log("Target object is valid!");
        }
        else
        {
            Debug.Log("Target object is not valid or has been destroyed.");
        }

        // Shuffle the array
        Utilities.ShuffleArray(numbers);

        // Print shuffled array
        Debug.Log("Shuffled Array: " + string.Join(", ", numbers));
    }
}

Last updated