Networked spawn manager fails to assign certain values to spawned object

I have a network player instantiator set on an empty game object with support for 2 players. The values i assign to let the gameobjects know what is what in the scene. Both the players are spawned correctly but the values i want to assign do not get passed to Player 2 and sometimes Player 1. What am i doing wrong and how can the script be improved? EDIT:the problem might lie into the fact that there is a small difference to the time each player loads the scene. EDIT2:The console shows all the debug log messages , even for player 2 , wtf is going on here

    var players:NetworkPlayer[];
var player1Prefab:GameObject;
var player2Prefab:GameObject;
var player1SpawnPos:GameObject;
var player2SpawnPos:GameObject;
var spawnedObject:GameObject;
var spawnedObject2:GameObject;

// var spawnSheet:GameObject; var musicGameObject:GameObject; var GUITextObject:GameObject; var GUIExcellentObject:GameObject; var detectionManager:GameObject;

function OnLevelWasLoaded(){
Debug.Log("ONLEVELLOAD");

if(Network.isServer){

Debug.Log("network is server");
spawnedObject=Network.Instantiate(player1Prefab,player1SpawnPos.transform.position,player1SpawnPos.transform.rotation,0);
Debug.Log("i spwned you");
detectionManager.GetComponent("SharedDetectionManagerScript").player1=spawnedObject;
Debug.Log("i assigned you to the shared detection manager");
spawnedObject.GetComponent("NetworkedDetectionScript").sharedDetectionManager=detectionManager;
Debug.Log("showed you who the detection manager is");
spawnedObject.GetComponent("NetworkedDetectionScript").spawnSheet=player1SpawnPos.transform;
Debug.Log("assigned Spawn Sheet");
spawnedObject.GetComponent("NetworkedDetectionScript").musicGameObject=musicGameObject;
Debug.Log("Assigned music game object");
spawnedObject.GetComponent("StageCompletedScript").GUITextObject=GUITextObject;
Debug.Log("assigned text object");
spawnedObject.GetComponent("StageCompletedScript").GUIExcellentObject=GUIExcellentObject;
Debug.Log("assigned gui excellent object");
spawnedObject.GetComponent("StageCompletedScript").musicGameObject=musicGameObject;
Debug.Log("assigned music game object");
}
if(Network.isClient){
 Debug.Log("P2 network is client");
spawnedObject2=Network.Instantiate(player2Prefab,player2SpawnPos.transform.position,player2SpawnPos.transform.rotation,1);
Debug.Log("P2 i spwned you");
detectionManager.GetComponent("SharedDetectionManagerScript").player2=spawnedObject2;
Debug.Log("P2 i assigned you to the shared detection manager");
spawnedObject2.GetComponent("NetworkedDetectionScript").sharedDetectionManager=detectionManager;
Debug.Log("P2 showed you who the detection manager is");
spawnedObject2.GetComponent("NetworkedDetectionScript").spawnSheet=player2SpawnPos.transform;
Debug.Log("P2 showed you who the SPAWN SHEET is");
spawnedObject2.GetComponent("NetworkedDetectionScript").musicGameObject=musicGameObject;
Debug.Log("P2 showed you who the music game object is");
spawnedObject2.GetComponent("StageCompletedScript").GUITextObject=GUITextObject;
Debug.Log("P2 showed you who the gui text objectr is");
spawnedObject2.GetComponent("StageCompletedScript").GUIExcellentObject=GUIExcellentObject;
Debug.Log("P2 showed you who the excellent object is");
spawnedObject2.GetComponent("StageCompletedScript").musicGameObject=musicGameObject;

}

}

function OnPlayerDisconnected(player: NetworkPlayer) {
//Debug.Log("Clean up after player " + player);
Network.RemoveRPCs(player);
Network.DestroyPlayerObjects(player);
}

When using Network.Instantiate the object get spawned on your PC. It also sends a buffered RPC call to all others and triggers the spawning of this object. Now we have a new instance of that prefab on every client (the server included) but they aren't related in any way.

By attaching a NetworkView component to the prefab the unrelated objects on the different PCs get a relationship. Network.Instantiate also syncs the NetworkViewIDs of all attached NetworkViews. A NetworkView is responsible for syncing data across the network. What kind data depends on the observed property of your NetworkView. If you drag&drop the transform component, the networkview will sync the position/rotation (/and i guess scale) so that it matches the position/rotation of the owner object.

If there is a script attached to the object and you want to sync variables/data from this script, you have to use a NetworkView that observes the script component and implement the OnSerializeNetworkView function to serialize/deserialize the desired data.

But besides the way of syncing data between clients you have to take a look at what data you want and can sync. The Serialize() function of BitStream used in OnSerializeNetworkView can only send a few datatypes. In general it's not possible and it wouldn't make sense to send instance references across the network. A instance reference is only valid on the client that created that object.

If you have a manager script on every client, all those objects are unique and you can't pass a reference to this manager script on your PC to another PC. For managers use some kind of a singleton - pattern so you have easy global access (on one PC).

If you need to sync some data of the manager you can use a RPC call to sync your desired data yourself.

You should also take a closer look at the NetworkView component:

http://unity3d.com/support/documentation/Components/class-NetworkView.html

final hints: The player that called Network.Instantiate owns the created object and act as the synchronisation-master. The NetworkViewIDs that are generated due to instantiate are owned by the calling NetworkPlayer. RPC calls work in both directions. To be able to send RPC function calls you don't need to observe a component. Just turn NetworkView.stateSynchronization off if you use the NetworkView only for RPC calls.

There's not much left i can tell you about network-syncing in unity. If you have problems implementing OnSerializeNetworkView or how to create a singleton just take a look at some other questions around here, or create a new question.

good luck

I think i got your problem. It's just because you use dirty JS. Didn't you get some error messages? If not i recommend to use C# :D

JS is not really type save but your problem is probably that you didn't cast your scripts into the right types. GetComponent returns a reference of type Component. You have to cast it to your script type.

And don't call GetComponent that much. It's a performance killer. Use typed local variables that will do the cast implicit.

function OnLevelWasLoaded(){
   Debug.Log("ONLEVELLOAD");
   if(Network.isServer){
      Debug.Log("network is server");
      spawnedObject=Network.Instantiate(player1Prefab,player1SpawnPos.transform.position,player1SpawnPos.transform.rotation,0);
      var SDManager : SharedDetectionManagerScript = detectionManager.GetComponent("SharedDetectionManagerScript");
      SDManager.player1=spawnedObject;
      var NDScript : NetworkedDetectionScript = spawnedObject.GetComponent("NetworkedDetectionScript");
      NDScript.sharedDetectionManager=detectionManager;
      NDScript.spawnSheet=player1SpawnPos.transform;
      NDScript.musicGameObject=musicGameObject;
[...]

To avoid the cast you can use the generic function of GetComponent()

spawnedObject.GetComponent.<NetworkedDetectionScript>().sharedDetectionManager=detectionManager;