- Home /
The question is answered, right answer was accepted
Network.Instantiate Not Correctly Spawning(Multiplayer)
I haven't been able to figure this one out. From everything that I have read so far Network.Instantiate should create a prefab on everyone(server and client). Unfortunately, that hasn't been the case.
What I want to have happen is when an enemy is spawned have both players be able to see it and interact with it. (EX: do damage, take damage, etc.) What is happening right now is an enemy is spawned but only one player can see it and interact with it. The enemy will chase and interact with both players but only one player can see them. Its super confusing.
Here is the code that I am using on a object in the scene:
var spawnPoints : Transform[]; // Array of spawn points to be used.
var enemyPrefabs : GameObject[]; // Array of different Enemies that are used.
var amountEnemies = 20; // Total number of enemies to spawn.
var yieldTimeMin = 2; // Minimum amount of time before spawning enemies randomly.
var yieldTimeMax = 5; // Don't exceed this amount of time between spawning enemies randomly.
function Start(){
//networkView.RPC("Spawn", RPCMode.All);
Spawn();
}
function Spawn(){
if(Network.isServer && networkView.isMine){//I only want the "server" player to do the instantiation
for (i=0; i<amountEnemies; i++){ // How many enemies to instantiate total
yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax)); // How long to wait before another enemy is instantiated.
var obj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.length)]; // Randomize the different enemies to instantiate.
var pos: Transform = spawnPoints[Random.Range(0, spawnPoints.length)]; // Randomize the spawnPoints to instantiate enemy at next.
Network.Instantiate(obj, pos.position, Quaternion.identity,0); //Spawn enemy for all players
}
}
}
Any help one this one would be greatly appreciated. Oh! Also as a side note a network view is attached to the gameobject that has this script on it.
I see nothing wrong with the code, are you sure this function is called when all the players are connected? Because Network.Instantiate doesn't work (AFAI$$anonymous$$) as a buffered RPC, so if a player connects after this function is called, he won't receive instantiations.
I searched a little and everywhere I'm looking at, Network.Instantiate is treated as a buffered RPC, so that shouldn't be the problem.
Thanks for the reply. So I am moving everyone from one area to another and when the server player connects that's when this should call. The server player connects after the client player(in my tests). You're saying though that I can't call this function on Start()? I have to call this OnPlayerConnected() or OnConnectedToServer()?
EDIT: I am wondering if I could achieve the same results as above except make it Instantiate not Network.Instantiate through an RPC call like:
Start(){
networkView.RPC("Spawn", RPC$$anonymous$$ode.All);
}
@RPC
Spawn(){
Instantiate(enemy, location, rotation, 0);//example
}
"The server player connects after the client player" I don't really understand what you're doing. A client is supposed to connect to a server and not the contrary :p . In my tests, using Network.Instantiate failed to work as a "buffered rpc" while it is documented as if.
What you're doing with a RPC call should work if you use RPC$$anonymous$$ode.AllBuffered, buffered RPC means the RPC call will stay in a buffer and if a new client connects, he will receive this rpc, so yeah replace with AllBuffered and it should do what you want. An object with a networkView instantiated will always have the Server as owner by default.
Ya i wrote that wrong lol. I did mean that the client connects to the server not the other way around. Okay thats great to know. I will try the RPC$$anonymous$$ode.AllBuffered and see if that doesn't fail me like Network.Instantiate.
Answer by N1warhead · Dec 17, 2014 at 06:43 PM
not sure if it's like Photon, if it is you can try something like this
JUST AN EXAMPLE (May or may not work)
GameObject Clone;
Clone = Network.Instantiate(obj, pos.position, Quaternion.identity,0) As GameObject; //Spawn enemy for all players
Well I tried it like you had it and it worked. I have no idea what was different... $$anonymous$$aybe it was a server and client connection issue. I guess I will have to test this further. The following it the code that I used and it worked.
public var Enemy : GameObject;
public var SpawnLoc : GameObject;
function Spawn()
{
var clone : GameObject;
clone = Network.Instantiate(Enemy, SpawnLoc.transform.position, Quaternion.identity, 0) as GameObject;
}
function OnGUI(){
if(GUI.Button(Rect(10,Screen.height-80, 100, 50), "<size=20>Spawn</size>"))
{
Spawn();
}
}
Sorry didn't even realize you were using Unityscript lol.
But glad you was able to convert it (as I don't know Unityscript).
But yeah, you have to declare a gameobject then for each instantiation create a new gameobject from the variable.
I know it's ridiculous, but I had that same exactly problem with Photon lol.
And it works. I don't know why it can't be simpler, but it's not.
But anyhow, I'm glad it works buddy!
Follow this Question
Related Questions
Late join Auth Server Instantiation - ViewID issues? 0 Answers
Network.Instantiate crashes my game 1 Answer
Instantiate different player models using Unet 0 Answers
How to let the other network player to see a launched rocket and its explosion? 1 Answer
How do I Network.instantiate an object based on Playerprefs? 0 Answers