'SendMessage Error - Please help

Hi, i’m using a mixture of code from the networking examples and from M2H (http://www.M2H.nl)

I’m not a network person, so trying to clue this together! Basicly I want someone to be able to ‘Connect’ and on doing so it creates the object in the scene.

I’m getting a

‘Assets/Connect.js(44,36): BCE0019: ‘SendMessage’ is not a member of ‘UnityEngine.Object’.
M2H (http://www.M2H.nl)’

error in the editor at the moment and don’t know what it means or what is wrong with the code.

Code is below, please could someone help me!!..

/* 
*  This file is part of the Unity networking tutorial by M2H ([url]http://www.M2H.nl[/url])
*  The original author of this code Mike Hergaarden, even though some small parts 
*  are copied from the Unity tutorials/manuals.
*  Feel free to use this code for your own projects, drop me a line if you made something exciting! 
*/
#pragma strict

var remoteIP = "127.0.0.1";
var remotePort = 25001;
var listenPort = 25000;
var useNAT = false;


//Obviously the GUI is for both client&servers (mixed!)
function Awake() 
{
	if (FindObjectOfType(ConnectGuiMasterServer))
		this.enabled = false;
}
function OnGUI ()
{

	if (Network.peerType == NetworkPeerType.Disconnected){
	//We are currently disconnected: Not a client or host
		GUILayout.Label("Connection status: Disconnected");
		

		GUILayout.BeginVertical();
		if (GUILayout.Button ("Connect as client"))
		{
			//Connect to the "connectToIP" and "connectPort" as entered via the GUI
			//Ignore the NAT for now
			Network.useNat = useNAT;
			Network.Connect(remoteIP, remotePort);
		}
		
		if (GUILayout.Button ("Start Server"))
		{
			Network.useNat = useNAT;
			Network.InitializeServer(32, listenPort);
			// Notify our objects that the level and the network is ready
			for (var go in FindObjectsOfType(GameObject))
				go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);	

		}
		GUILayout.EndVertical();
		
		
	}else{
		//We've got a connection(s)!
		

		if (Network.peerType == NetworkPeerType.Connecting){
		
			GUILayout.Label("Connection status: Connecting");
			
		} else if (Network.peerType == NetworkPeerType.Client){
			
			GUILayout.Label("Connection status: Client!");
			GUILayout.Label("Ping to server: "+Network.GetAveragePing(  Network.connections[0] ) );		
			
		} else if (Network.peerType == NetworkPeerType.Server){
			
			GUILayout.Label("Connection status: Server!");
			GUILayout.Label("Connections: "+Network.connections.length);
			if(Network.connections.length>=1){
				GUILayout.Label("Ping to first player: "+Network.GetAveragePing(  Network.connections[0] ) );
			}			
		}

		if (GUILayout.Button ("Disconnect"))
		{
			Network.Disconnect(200);
		}
	}
	

}

// NONE of the functions below is of any use in this demo, the code below is only used for demonstration.
// First ensure you understand the code in the OnGUI() function above.

//Client functions called by Unity
function OnConnectedToServer() {
	Debug.Log("This CLIENT has connected to a server");	
}

function OnDisconnectedFromServer(info : NetworkDisconnection) {
	Debug.Log("This SERVER OR CLIENT has disconnected from a server");
}

function OnFailedToConnect(error: NetworkConnectionError){
	Debug.Log("Could not connect to server: "+ error);
}


//Server functions called by Unity
function OnPlayerConnected(player: NetworkPlayer) {
	Debug.Log("Player connected from: " + player.ipAddress +":" + player.port);
}

function OnServerInitialized() {
	Debug.Log("Server initialized and ready");
}

function OnPlayerDisconnected(player: NetworkPlayer) {
	Debug.Log("Player disconnected from: " + player.ipAddress+":" + player.port);
}


// OTHERS:
// To have a full overview of all network functions called by unity
// the next four have been added here too, but they can be ignored for now

function OnFailedToConnectToMasterServer(info: NetworkConnectionError){
	Debug.Log("Could not connect to master server: "+ info);
}

function OnNetworkInstantiate (info : NetworkMessageInfo) {
	Debug.Log("New object instantiated by " + info.sender);
}

function OnSerializeNetworkView(stream : BitStream, info : NetworkMessageInfo)
{
	//Custom code here (your code!)
}

/* 
 The last networking functions that unity calls are the RPC functions.
 As we've added "OnSerializeNetworkView", you can't forget the RPC functions 
 that unity calls..however; those are up to you to implement.
 
 @RPC
 function MyRPCKillMessage(){
	//Looks like I have been killed!
	//Someone send an RPC resulting in this function call
 }
*/

Is the part “M2H (http://www.M2H.nl)” really in Unity’s error message? If so; make sure to comment the “M2H” message in the comments at the top.

However, the real reason of your error is that I defined this script to use “#pragma strict”. It’s more strict when compiling, but forces you to write typed variables.

The fix is to change the offending lines to:

for (var go : GameObject in FindObjectsOfType(GameObject))
            go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);   

      }

…you can also choose to remove “#pragma strict” for now.

The problem on line 44 is that FindObjectsOfType returns an array of Object, but SendMessage is actually a method of the GameObject class (ie, the compiler doesn’t know that the go variable is actually going to represent a GameObject at runtime). You just need to change the line to this:-

(go as GameObject).SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);

Thanks, that worked.

What I have happening now is that I start the world in Unity using the server option, then within a browser connect as a player.

What I wanted to happen was to have a ‘player’ added to the world that the user could control and move around other players that had also joined.

What is happening is that the ‘player’ is added’ but the server moves and controls both players (Or as many that join).

The code for the this is below. What do I need to change? is it because the server is controling all players?

Thks

public var playerPrefab : Transform;
public var playerScripts : ArrayList = new ArrayList();

function OnServerInitialized(){
	//Spawn a player for the server itself
	Spawnplayer(Network.player);
}

function OnPlayerConnected(newPlayer: NetworkPlayer) {
	//A player connected to me(the server)!
	Spawnplayer(newPlayer);
}	

	
function Spawnplayer(newPlayer : NetworkPlayer){
	//Called on the server only
	
	var playerNumber : int = parseInt(newPlayer+"");
	//Instantiate a new object for this player, remember; the server is therefore the owner.
	var myNewTrans : Transform = Network.Instantiate(playerPrefab, transform.position, transform.rotation, playerNumber);
	
	//Get the networkview of this new transform
	var newObjectsNetworkview : NetworkView = myNewTrans.networkView;
	
	//Keep track of this new player so we can properly destroy it when required.
	playerScripts.Add(myNewTrans.GetComponent(Tutorial_3_Playerscript));
	
	//Call an RPC on this new networkview, set the player who controls this player
	newObjectsNetworkview.RPC("SetPlayer", RPCMode.AllBuffered, newPlayer);//Set it on the owner
}



function OnPlayerDisconnected(player: NetworkPlayer) {
	Debug.Log("Clean up after player " + player);

	for(var script : Tutorial_3_Playerscript in playerScripts){
		if(player==script.owner){//We found the players object
			Network.RemoveRPCs(script.gameObject.networkView.viewID);//remove the bufferd SetPlayer call
			Network.Destroy(script.gameObject);//Destroying the GO will destroy everything
			playerScripts.Remove(script);//Remove this player from the list
			break;
		}
	}
	
	//Remove the buffered RPC call for instantiate for this player.
	var playerNumber : int = parseInt(player+"");
	Network.RemoveRPCs(Network.player, playerNumber);
	
	
	// The next destroys will not destroy anything since the players never
	// instantiated anything nor buffered RPCs
	Network.RemoveRPCs(player);
	Network.DestroyPlayerObjects(player);
}

function OnDisconnectedFromServer(info : NetworkDisconnection) {
	Debug.Log("Resetting the scene the easy way.");
	Application.LoadLevel(Application.loadedLevel);	
}

Does anyone know how to help with this?