GameObject Clone problem

So i recently started making a map with Udon in which I will be putting some different weapons.
I came far with making a gun and making proper bullets for it , but when I came to destroying(setting them to inactive) those bullets things didn’t go as smooth as I hoped they would.
So currently my bullets spawn as a clone of a GameObject prefab and with velocity of their rigidbody get thrown forward.

(this notes are from the Gun(GameObject that fires the bullets))

And also here I have the Udon notes for the bullets time to disappear.

So from what i have seen that the problem is that the clones of a bullet take the information about disappearing from the original. So if the original will disappear after 10 seconds of its existence they will do the same and not use information about their time of existence. So basically what Im looking for is a way for clones to not take information from their originals interactions ,but from their own.

If anybody has a answer to this please let me know in the replays.
Thank you so much for reading and have a good rest of your day.

Mmmhh…

I dont use the Udon Graph but as far as I know instantiated Objects should run their own copy of the code.

My guess would be that when you set the bullet to inactive the reference you’re using referes to the original bullet instead of itself.

In Code I’d probably use this.GameObject to refer to itself instead of using a set GameObject “variable” for it. Idk how its called in Graph tho.

One more Thing - you should probably destroy the instantiated Bullets instead of turning them inactive to save on performance. ← might be true (idk for sure) for instantiated objects, but like Luna said after my post, its better to just have Objects already in and enable/ disable them

Edit: Oh, and another. If you want other players to see the bullets too, not just the local player, you need to instantiate the bullets inside of an network Event.
Otherwise Your Bullets will only be visible for the local Player.

1 Like

unrelated but this should help with performance and logic.
When doing things like bullets, please use an object pooler.

Put simply, you make an array with your bullet prefab and you move your bullets into place when you use them. (or activate/deactivate the game objects).

Don’t just spawn the prefabs and destroy them again. This is very bad for unity’s and by extension your world’s performance. Since it’ll have to keep creating and destroying objects.

It’s usually best practice to just make an array of roughly the amount you’ll need on screen from the gun at any given moment and hide them until you need them.

1 Like