Instantiate/Spawn Props?

Hi all, I’m working on my first World and am trying to spawn props from a UI menu.

I’ve figured out the UI canvas and everything else, but I’m having trouble wrapping my mind around the actual instantiation/spawning of prop objects. I thought I had them figured out, but when testing the world with a friend, we realized neither of us could see the other one’s pickup objects.

Some research looks like I should be using object pooling, but I just can’t get it to synch right.

For example, I want to have a sort of “Bar Menu” where the player can order drinks. I want these drinks to have VRC_Pickup on them, and I’ve stored them inside some other geometry with their colliders disabled (so they don’t get flung out into space) and with their rigidbody.useGravity = false so they don’t fall into the void. I’m also setting them Inactive after the object pooler starts up.

Maybe I’m approaching this the wrong way, but, what I want is:

  1. Player approaches one of four “Spawn Terminals” (which have the menu)
  2. Player selects a drink from the menu
  3. The drink prop gets moved to the spawn area for that “terminal”
  4. The prop gets set to Active, and has its collider/useGravity enabled

As it stands currently, I’ve managed to get a single pooled object to appear (only when the Master selects it from the menu). The prop never gets its gravity enabled. Another player can grab the prop and move it, but the new position doesn’t update on the Master, and as soon as the Master grabs his version of the prop, it appears to teleport away from the other player.

Maybe I’m just not understanding how to synch or send network events correctly. I tried using the EXUR object pooler, but that doesn’t look like it handles one of my core requirements: Spawning an object from a common pool at one of many spawn points depending on which menu the player chooses

So, in general, don’t spawn items.
Instead, you should use object pooling. Which basically means you create an array with the number of items you think you’ll need and hide them or store them somewhere that can’t be seen.
Once you need one, you can just grab one from the pool and move it into position.

Why? Well, creating and destroying a lot of items can be taxing on unity, not to mention it can be prone to desync.

As for object syncing. By default, the position of a object is local. If you want the position to update, you’ll need to add an object sync. if you’re using Udon# there’s a button for it on the script itself.

TL.DR of the logic behind everything in U# (note the following bit is example code and not actually working code. it’s just as an illustration of logic)

public GameObject[] drinks1;
public GameObject spawnLocation;
public int poolPosition = 0;
public override void OnInteract(){
drinks1[poolPosition].transform = spawnLocation.transform;

I’d just store the drinks above or under the map, or maybe in a cabinet underneath, and have each drink have an empty UdonBehaviour with ObjectSync turned on.

I’ve gotten it more or less working (baby steps, I’ve taken a step back and gone back through “can I sync these cubes” and “can I pull them from the pool” etc.), thanks for the help.

It turns out one of my biggest problems was not properly syncing which object from the pool I was pulling. At least, I think that explains some of the weird behavior with props teleporting around when the Master moved them. Either way, by tearing out the old code and going back to square zero it seems to have fixed whatever I did wrong.

It also looks like, and I might be wrong, that SDK3 doesn’t properly let me toggle the rigidbody (either useGravity or isKinematic). Testing in CyanEmu shows it working as expected, but when I “Build and Test” nothing changes. It looks like SDK2’s VRC_ObjectSync had some methods for changing these things, am I right that it’s not currently implemented in SDK3?

Totally open to ignoring and working around that limitation (it’s not critical), I just don’t know if I’m missing another quirk of Udon and VRChat.

With sdk2 the state of objects was synchronized more. With SDK3 you’ll have to do network events to sync the proporties on objects, since they’re not synced by default, only the position, rotation and velocity is.