Toggle Material Swap
Swap between two materials on interaction. Useful for theme modes or state indicators.
Category: Utility Tools
UdonSharp Script
using UdonSharp;
using UnityEngine;
using VRC.Udon;
public class ToggleMaterialSwap : UdonSharpBehaviour
{
public Renderer targetRenderer;
public Material materialA;
public Material materialB;
private bool useA = true;
private void Start()
{
ApplyMaterial();
}
public override void Interact()
{
useA = !useA;
ApplyMaterial();
}
private void ApplyMaterial()
{
if (targetRenderer == null || materialA == null || materialB == null) return;
targetRenderer.material = useA ? materialA : materialB;
}
}
Setup
- Add script to an interactable object.
- Assign renderer and two materials.
- Add
VRC_Interactable.
Notes
- This version is local-only.
- Convert to synced bool if you want global state.
Extra Tips and Troubleshooting
Tips and Tricks
- Use material swaps for quick accessibility modes and theme toggles.
- Keep material properties aligned to avoid sudden visual artifacts.
Troubleshooting
- If material does not change, verify renderer reference and both material slots.
Related Content
Prefab Setup Notes
Import the prefab or script into a throwaway test scene before adding it to a live world. Confirm inspector references, ownership behavior, sync, triggers, UI hooks, and audio or object links before moving it into the production scene.
Testing Checklist
- Test once as a local player and again with a second client or late joiner if the behavior can affect more than one player.
- Confirm ownership, sync, trigger zones, UI references, and audio or object references are assigned intentionally.
- Check desktop and VR interaction distance so players can actually use the feature in context.
- Keep a backup of the scene before changing prefabs, UdonBehaviours, or serialized references.
