Enabling and Disabling objects over the network

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

Enabling and Disabling objects over the network

Vallar
2018-07-08 11:21:28

Hi everyone,

First, sorry if this has been asked before but my search yielded nothing useful to me so I thought I'd ask.

Basically my issues is finding out how to enable and disable objects (both spawned and not spawned) over the network that don't have a PhotonView on them. Basically imagine this, I have a resource in the world that players gather. I need to disable them and re-enable them when I want (for performance reasons).

I tried using PunRPC to disable/enable and I got an error message that they don't serialize GameObjecs (I was passing the target GameObject to the RPC to disable it). I then tried to use RaiseEvent for the same thing and pass the GameObject in "content" but still, got the same error message.

So how do I disable an object I didn't spawn over the network and how do I disable an object I spawned over the network in PUN?

Thanks in advance.

Comments

JohnTube
2018-07-09 09:24:00

Hi @Vallar,

I tried using PunRPC to disable/enable
Yes that is the way to go.
I got an error message that they don't serialize GameObjecs (I was passing the target GameObject to the RPC to disable it)
You should exchange some kind of unique reference or ID or pointer to the GameObject. Like its name (string) if it's the same on all clients. etc. Read about Photon Serialization and supported or custom types here.
I then tried to use RaiseEvent for the same thing
You should know that PunRPC is RaiseEvent under the hood.

Vallar
2018-07-09 16:23:12

Thanks for your reply @JohnTube

You should exchange some kind of unique reference or ID or pointer to the GameObject. Like its name (string) if it's the same on all clients. etc.

I am guessing that if I pass in a string name, I would have to do a GameObject.Find() to find the game object using that string, right? Unfortunately GameObject.Find() is a bad practice and should be avoided at all costs. Specially so if it is going to be run continuously. Unless of course there is another way to get an object by name I am not aware of in Unity that is far efficient than GameObject.Find() and its ilk.

Note that my use case pertains a resource that is being hunted by players on a regular basis.

In any case, I found another way, I just pooled the objects and shared their list's index around using events and used that to disable the correct object using RaiseEvent.

Back to top