Instantiating and isMine for player controls

Hi there.

Let me start out by saying, I do realise theres a million posts regarding the issues I’m having, but I can’t seem to find anything useful for a solution.

I am currently trying to add multiplayer functionality to a game I’m developing. I have been a lot of tutorials through and a lot of these were great to help me get the server up and running. I am not using Photon, MasterServer or anything similar, but the connection works properly with NAT punch-through and everything.

I finally got around to instantiating my players so that each client could see the other clients’ added players, only problem now is multiple cameras and controls are not individual to the client, but the latest added player is controlling all of the other players and the other players’ cam are the one of the last added player.

So in order to find a method of getting this to work, I started all over again. So I set up a server (locally this time) and tested that clients can connect. I then instantiate a simple cube with the OnPlayerConnected() method, which also works great. The cube has a networkView attached as well as a script, which creates a GUI.Button, allowing it to change color with the “gameObject.renderer.material.color = new Color(x,x,x)”. I’ve set this up so when the button is pressed, it will ask the server through a RPC to change the color of the cube.
The cube is a prefab, from which I’ve deleted the camera, because I found that it should not be there, but be added through scripting when the ‘player’ (cube) is instantiated.

So now I have a test scenario, where there is a main camera and a light source. The first instance of the game is used to set up the server. Next, I join with a client and see a clone of my prefab be instantiated. It also adds the “change color” button and it even works. so far so good.

But when I join the server with another client, it adds another clone of my prefab (which it should), but the change color function only changes the color of the first instantiated prefab. And it is the same story no matter how many clients I would join the server with.

I figured that the reason might be because I ask the server to change the color, but even if I add a function (not an RPC) to change the color, it is still only changing the color of the first cube.

Now i’m going crazy because I can’t even get my stupid cubes to change color in my very simple test scenario. Does anyone know what could be the reason for this?

The code I’m using is the same as in this example:

I have tried adding the if(networkView.isMine), which I haven’t had much luck with, but it seems that it only returns true from the server, so it seems that it is the server that has ‘owns’ the cube, which isn’t my intention, but I guess this is happening because I ask the server to instantiate the prefab. Is this a correct assumption?

I’ve been through a lot of examples and decided this morning to try with the Network.Instantiate() method instead of instantiating through an RPC (see the example above with Network.AllocateViewID). It gives me the same result however, not allowing me to change the color of the second instantiated cube.

So now I am very confused and don’t know how to continue, so any help would be very much appreciated.

I am expecting that whenever I get the second cube to be able to change color from the client that has instantiated it, then I can translate that to my game and thereby make the controls work for each client instead of the latest added controlling all players at once.
When this is done, my next issue will be managing multiple cameras, which I’ve already tried to look into, but I can’t find any good approaches here either. So furthermore, if any of you guys have good links, ideas or thoughts, please do share :slight_smile:

I read this yesterday (a little late, judging on when I began working on this, but it helped clarify a bit about networkView and other things as well): http://docs.unity3d.com/Documentation/Components/NetworkReferenceGuide.html

Here’s links to the scripts I’m using right now:
ChangeColor.cs (attached to the cube prefab): changeColor.cs - Pastebin.com
GameMenu.cs (attached to the main camera): GameMenu.cs - Pastebin.com
Spawn.cs (also attached to the main camera): Spawn.cs - Pastebin.com

Thanks in advance.

  • J

OnPlayerConnected is server side, OnConnectedToServer is client side. The instance that instantiates the object is the owner.

you have got to be kidding me…

Thank you so much dude.