Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Dec 27, 2016 at 07:40 PM by wesleywh for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by wesleywh · Dec 15, 2014 at 03:40 AM · network.instantiate

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.

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ijatsu · Dec 15, 2014 at 10:59 AM 0
Share

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.

avatar image Ijatsu · Dec 15, 2014 at 02:14 PM 0
Share

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.

avatar image wesleywh · Dec 15, 2014 at 04:20 PM 0
Share

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
     }


avatar image Ijatsu · Dec 17, 2014 at 07:10 AM 1
Share

"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.

avatar image wesleywh · Dec 17, 2014 at 06:26 PM 0
Share

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.

1 Reply

  • Sort: 
avatar image
0
Best Answer

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
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wesleywh · Dec 17, 2014 at 07:03 PM 0
Share

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();
     }
 }
avatar image N1warhead · Dec 17, 2014 at 07:06 PM 0
Share

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

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges