Multiplayer Cameras Problems

Hi, i'm new to unity, i'm playing with racing game tutorial, and trying to add multiplayer capabilities to it, i followed M2H network tutorial, and seem to understand it (to some extent), I'm creating car objects by Network.Instantiate also i modified Car.js, so that car would be controllable only by object owner

function GetInput()
{
    if (networkView.isMine) {
        throttle = Input.GetAxis("Vertical");
        steer = Input.GetAxis("Horizontal");
        CheckHandbrake();
    }
}

and this part seems to work. The problem that each car has a camera object assigned and camera has component CarCamera which needs target and is responsible for changing angles depenging on the car position, and when i create second car and second camera first camera loses it's focus and second one doesn't get one, so as a result while cars are individually controlled game cannot be played, because of that camera issue.

The script i use for instaniating players is:

function SpawnPlayer(){
    var player : Transform = Network.Instantiate(networkCarPrefab, Vector3(860, 102.2085, 878), transform.rotation, 0);
    var camera : Transform = Network.Instantiate(Main_Camera, Vector3(860, 102.2085, 878), transform.rotation, 0);
    var carCamera = camera.GetComponent("CarCamera");
    carCamera.target = player;
}

Would anyone point me into direction i should be digging ? Thx

As long as the camera's target is a non static member variable (ie it's declared outside any functions, at the very top of the script file as just var target : GameObject; or similar) you can assign your car to that in the Start or Awake events. Just use GetComponent("CarCamera").target = whatever, or use GetComponentInChildren if you parent the camera to the car's master game object. So just move your target variable outside any functions (and you may want to move the camera variable out too). Declare them at the top of the file using var name : GameObject; and then populate them further down.

Bear in mind that you'll want to declare the camera's target variable in its own script file attached to your camera, not the car, so look there for it instead of in your own spawn file.

While, Cap provided some usefull insights, that was not the case.

The way i solved the problem, was not to instaniate a seperate camera object for each player, but to use one camera and to assign target for it on each player spawn. so the working code in my case looks like:

function SpawnPlayer(){
    var player : Transform = Network.Instantiate(networkCarPrefab, Vector3(860, 102.2085, 878), transform.rotation, 0);
    var camera = GameObject.Find("Main_Camera");
    var follow = camera.GetComponent("CarCamera");
    follow.target = player;
}