Set NetworkViewID manually

We want to create a Multiplayer-Game with multiple Game Instances. In that case that every Game Instances needs to have its own NetworkViewID I have the following question:

Is it possible to set the NeworkViewID manually per script. I think about something like that:

NetworkView.ViewID = 10;

Is this possible to do, because I don’t like to use the Network.AllocateViewID() function. Ah btw I’m writing in C#.

NetworkViewID’s are the classes that store the information necessary for for a NetworkView. You can’t set the number for this ViewID because Unity needs to make sure that the view is allocated and available. The issue that you think you’re having is not an issue at all.

You are going to have multiple clients connecting to your server, that’s what the NetworkViewID’s are for! They’ll work perfectly, but you need to know how to set them up properly. I’ll explain the example from the link I posted in the comments to help you better understand how these work and how you can manipulate them.

public Transform cubePrefab;
void OnGUI() {
	if (GUILayout.Button("SpawnBox")) {
		NetworkViewID viewID = Network.AllocateViewID();
		networkView.RPC("SpawnBox", RPCMode.AllBuffered, viewID, transform.position);
	}
}

First is this bit of code.

There is a button displayed on the GUI that says “SpawnBox” and, when clicked, it sends a message to every connected application. Let me tear this function call apart for you so you can better understand it.

“SpawnBox” = This is the RPC function that is going to be called.

RPCMode.AllBuffered = This means that it will be called on every connected application and it’s buffered. This means that if the application connects after this function was called, it will call this function because it will be in the buffer and the new application needs to catch up.

The rest of the parameters are the parameters in the function that’s going to be called.

So this function is sending the viewID that was just allocated by Unity and the position that they want the box spawned at.

[RPC]
void SpawnBox(NetworkViewID viewID, Vector3 location) {
	Transform clone;
	clone = Instantiate(cubePrefab, location, Quaternion.identity) as Transform as Transform;
	NetworkView nView;
	nView = clone.GetComponent<NetworkView>();
	nView.viewID = viewID;
}

This is the function that’s going to be called on every application. Note that it has the [RPC] attribute set for the function; this is needed for the function to be called through the RPC function. It’s spawning a cube at the location provided from the application that sent the message and then assigns the viewID that was passed in from the other application to the NetworkView on the cube prefab.

This process makes sure that all the objects that are supposed to behave as the same object, have the same viewID on every client and the server.

I hope this clears up your confusion on the subject of NetworkViewIDs!

In Editor :
I Duplicate object that Contain networkView several Times. in each Duplication Unity Increase the ID number of ViewID. at last I Deleted the previous Object and Keep the New One. it has ID number different from 1.

this is a WAY. you can Choose another way :wink: