Instantiating an Object

Explanation:

  1. objectPrefab – This is the prefab that will be instantiated.

  2. spawnPoint – Defines where the new object should be spawned.

  3. VRCInstantiate(objectPrefab) – Creates a networked instance of the prefab.

  4. Setting position and rotation – Moves the new object to the desired location.

How to Use:

  • Attach this script to an interactable object (e.g., a button).

  • Assign a networked prefab to objectPrefab.

  • Set a spawnPoint where the object should appear.

  • Click on the object in VRChat to spawn the new instance.

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

public class ObjectSpawner : UdonSharpBehaviour
{
    public GameObject objectPrefab; // Assign the prefab in the inspector
    public Transform spawnPoint;    // Assign a spawn position

    public override void Interact()
    {
        if (objectPrefab != null)
        {
            GameObject newObject = VRCInstantiate(objectPrefab); // Clone the prefab

            if (newObject != null)
            {
                newObject.transform.position = spawnPoint.position;
                newObject.transform.rotation = spawnPoint.rotation;
            }
        }
    }
}

Last updated