Authoritative server problem

Hey guys, I’ve been following M2H’s networking tutorial and now I’m trying to make an authoritative server. I edited my player script so I can use it in multiplayer. Ok, one of the players makes a server and the other joins, that’s OK, but when the client joins the server, the server’s camera becomes the clients camera. The server still moves his own player and the client moves his own player, but the server player’s camera switches to the one of the client. Any ideas? I can post any of the code you need to see.

disable the script on the camera ( turn if off) and assign the target of your follow script in the setplayer rpc call:

@RPC
function SetPlayer(player : NetworkPlayer)
{
	owner = player;
	if(player==Network.player)
	{
		enabled=true;
		cam = GameObject.Find("Main Camera");
		sf = cam.GetComponent(SmoothFollow);
		sf.target = this.transform;
		sf.enabled = true;
	}
}

the server will send the rpc to the new player and that one will activate the script and assign itself as a target.

Thanks, but I have a problem:

var camObj = GameObject.Find("Camera"); 
var cam = camObj.GetComponent(CamControl); 
cam.enabled = true;

This is my code and it says that “enabled” is not a member of UnityEngine.Component. My camera object’s name is “Camera” and the script is called CamControl. What am I doing wrong than?

Is that code outside of any function ?

Try this in Awake :

camObj = GameObject.Find("Camera");
cam = camObj.GetComponent(CamControl);
cam.enabled = true;

Declare the variables outside of the function, of course. (var camObj : GameObject, and var cam : CamControl)

How does it work ?

No, of course it’s not outside of a function.
Here’s my code:

function Awake(){
rot = transform.rotation; 
	if(Network.isClient){
		enabled=false;
	}	
}

@RPC
function SetPlayer(player : NetworkPlayer){
owner = player;
	if(player==Network.player){
		enabled=true;
		var camObj = GameObject.Find("Camera"); 
		 var cam = camObj.GetComponent(CamControl); 
		 cam.enabled = true;
	}
}

I tried it in Awake and still the same error occurs.

Add :

if (camObj) Debug.Log (“There is a camObj”);
if (cam) Debug.Log (“There is a cam component”);

What does the console output ?

Nothing, the enabled error log just stays there and nothing else…it’s like there’s no debug.

I tried this in the Update function and everything was OK, the camera was there once I created the server and the server spawned the player:

if(GameObject.Find("Camera"))
{
print("there's a camera");
}

However, not sure if it’s correct to be written like this, but I tried this:
GameObject.Find(“Camera”).CamControl

and Unity said that CamControl (my script) is not a member…

Nothing pop in the console ?

Is there at least a gameObject called “Camera” in your scene ? :? It SHOULD work, it is one of the most basic code line, every project should have at least ONE !

Yeah, I said yes, the camera’s game object is there, but Unity can’t find it’s script for some reason…

Look:

if(GameObject.Find("Camera"))
{
print("there's a camera");
}
var cams = GameObject.Find("Camera");
if(cams.CamControl)
{
print("there's a component");
}

This thing says "Yes, there is a camera object, but Cam Control is not a member of the unity.GameObject.

:frowning:

this refers to a script called ‘CamControl’ that should be attached to the cam which i don’t think is the case.

  1. make sure you have only 1 cam in your scene.
  2. the client needs to set the target on the script to itself.
    (not the server or the cam will follow the last object that was instantiated)
    in my case i have the smoothfollow script attached to my camera and it’s disabled.
    when my player connects, the server sends the rpc to the connected client and basicly telling it to set the target on the script and activating it. So the cam should snap behind your client as soon as it’s spawned.
    you can also do this in the awake on a remote player gameobject

Yeah I saw that. I have only one camera in my level. Infact I don’t have any cameras until I spawn the server’s player. My script for my camera object is called “CamControl.js”. It’s a first person game, so my camera doesn’t have a target or something. It is just a child of the player. And I think this is the problem. The camera should be separated from the player and get attached to it.

So you’re saying that I should make a target and tell the camera to get attached to the target every time a client connects. That’s OK, but Unity just refuses to find my script, it keeps saying “CamControl is not a member of Unity.GameObject”.

maybe your calling the function before the gameobject is instantiated. my cam is in the scene from the start so when i call the rpc i’m sure the cam is there with the script.
In your case you could also disabled all cams found in the scene and enable only the one that you need. This also needs to be called from the client side.

EDIT:
My bad, I fixed it. Sorry for this guys!

No, it’s not fixed. Take a look at this:
GameObject.Find(“Camera”).GetComponent(CamControl).target = this.transform;

it finds the component, but it doesn’t find the target variable (I have a variable, called “target” in my camera script now).
It says that “target” is not a member of UnityEngine.Component :frowning: :frowning:

Why doesn’t it find the target variable?! It just makes no sense.