VRChat Udon Scripts

From ScottWiki
Jump to navigation Jump to search

Synced Object Toggle (with late joiners)

A useful collection of Udon# scripts for use in your VRC world.


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

public class ObjectToggleSync : UdonSharpBehaviour
{
    public GameObject target;

    public override void Interact()
    {
        SendCustomNetworkEvent(NetworkEventTarget.All, "ToggleTarget");
    }

public override void OnPlayerJoined(VRCPlayerApi player)
{
    if (Networking.IsMaster)
    {
        if(target.activeSelf)
        {
            SendCustomNetworkEvent(NetworkEventTarget.All, "ToggleTargetTrue");
        } else
        {
            SendCustomNetworkEvent(NetworkEventTarget.All, "ToggleTargetFalse");
        }
    }
}


    public void ToggleTarget()
    {
        target.SetActive(!target.activeSelf);
    }

    public void ToggleTargetTrue()
    {
        target.SetActive(true);
    }

        public void ToggleTargetFalse()
    {
        target.SetActive(false);
    }


}


OnEvent Dispense from Pool

using UdonSharp; using UnityEngine; using VRC.SDKBase; using VRC.Udon; using VRC.SDK3.Components; using VRC.Udon.Common.Interfaces;

public class OnEventDispenseSushi : UdonSharpBehaviour {

   public GameObject MenuButtonToDisable;
   public VRCObjectPool sushiPool;
   [SerializeField] GameObject SoundObject;
   private AudioSource AudioToHandle;
   public void DispenceSushiFromPool()
   {
       if (Networking.IsMaster == true)
       {
           if(!sushiPool.TryToSpawn())
               {
               SendCustomNetworkEvent(NetworkEventTarget.All, "DisableMenuButton");
               }
               else
                   {
                   AudioToHandle = SoundObject.GetComponent<AudioSource>();
                   AudioToHandle.Play();
                   }
       }
       else
           {
           SendCustomNetworkEvent(NetworkEventTarget.Owner, "DispenceSushiFromPool");
           }
       
   }
   public void DisableMenuButton()
   {
   MenuButtonToDisable.SetActive(false);
   }

}