How do I enable or disable a object for all players?

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.

How do I enable or disable a object for all players?

bebros2
2021-10-21 04:08:11

Specifically looking for something like a light switch where if I click a button on the keyboard it turns on a light. I'm pretty sure I have to use RPCs to do this but the photon documentation for this hasn't really helped.

I did manage to find one example on a discord server but it isn't really what I'm looking for but I will paste it in anyways. Here's the example.

photonView.RPC("DisableChildObject", PhotonTargets.AllBuffered, player, false);

[RPC]

void DisableObject(string ob, bool isActive)

{

GameObject.Find(ob).gameObject.SetActive(isActive);

}

Comments

[Deleted User]
2021-10-21 05:13:19

How is this not what you're looking for? You just want a way of disabling a GameObject for all the players in the room right? Also your RPC is called "DisableObject" but you're trying to send the RPC "DisableChildObject", so it's not going to be found.

Whenever you figure out what solution you want to go with, make sure it has a master client check on it or at least have a whitelist of which objects can actually be disabled. Giving people the power to disable any GameObject in the scene is dangerous and invites cheaters in.

bebros2
2021-10-21 23:53:52

MeepPun 2021-10-21T05:13:19+00:00

How is this not what you're looking for? You just want a way of disabling a GameObject for all the players in the room right? Also your RPC is called "DisableObject" but you're trying to send the RPC "DisableChildObject", so it's not going to be found.

Whenever you figure out what solution you want to go with, make sure it has a master client check on it or at least have a whitelist of which objects can actually be disabled. Giving people the power to disable any GameObject in the scene is dangerous and invites cheaters in.

So I ended up giving it a try by using the example and I wrote the thing below. I ended up getting a "Object reference not set to an instance of an object" error for the first line that calls the RPC. Any idea what I did wrong? Also even if this eventually starts working wouldn't GameObject.Find only work for objects that are active?

PV.RPC("LightOn", RpcTarget.AllBuffered, false);

[PunRPC]

void LightOn(string mainarealight, bool IsActive)

{

GameObject.Find("mainarealight").gameObject.SetActive(IsActive);

}

Do I replace gameObject with mainarealight?

[Deleted User]
2021-10-22 00:43:33

Look at your code, your RPC has two parameters but you're only sending a single parameter false. It should be:

Also, yes it will only work for active GameObjects. However, you don't need to use GameObject.Find. If you have a direct reference to the GameObject anywhere (perhaps in a static field), then you can just reference it from there. Or use the GameObject.tag property.

bebros2
2021-10-22 05:19:54

MeepPun 2021-10-22T00:43:33+00:00

Look at your code, your RPC has two parameters but you're only sending a single parameter false. It should be:

PV.RPC("LightOn", RpcTarget.AllBuffered, "string here", false);  

Also, yes it will only work for active GameObjects. However, you don't need to use GameObject.Find. If you have a direct reference to the GameObject anywhere (perhaps in a static field), then you can just reference it from there. Or use the GameObject.tag property.

Hey, sorry for bugging you again. I just don't think I'm really understanding this. Like what would the other parameter spot be used for since from the example above player isn't being used in the RPC so what is the point of including it. Also everything I've tried has led to the same object reference error and any examples online don't really help. Are you able to give me an example on how you would do this cause I'm really stumped and can't think of anymore solutions.

Tobias
2021-10-22 09:37:00

Actually, instead of using a buffered RPC, I would just add the light's state to custom room properties. This only carries the current state, it's available to everyone (even on join) and will not bloat.

It's hard to help with NullReferenceExceptions in any way, because we don't know your project and or scene. This is a classic C# debugging task, so if you need help with such things, please refer to C# docs.

Back to top