• 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 /
avatar image
Question by krakoui · Apr 22, 2020 at 03:50 PM · networkserverclient

Syncronization issues between server and client, using the OnStartServer() function,

Hi, I'm new to Unity, and I'm having troubles for synchronisation between server and clients. I am running the following code to spawn enemies at random positions.

//code starts here

using UnityEngine;

using UnityEngine.Networking;

public class EnemySpawner : NetworkBehaviour

{

 public GameObject enemyPrefab;

 public int numberOfEnemies;
 
 public override void OnStartServer()
 {
     for (int i = 0;i<numberOfEnemies; i++)
     {
         var spawnPosition = new Vector3(
             Random.Range(-8.0f, 8.0f),
             0.0f,
             Random.Range(-8.0f, 8.0f));

         var spawnRotation = Quaternion.Euler(
             0.0f,
             Random.Range(0,180),
             0.0f);

         var enemy = (GameObject)Instantiate(enemyPrefab,spawnPosition,spawnRotation);
         NetworkServer.Spawn(enemy);
         Debug.Log("spawn 1");
          
     }
 }

}

When I launch a game through the 'play' button in unity, then start to host a server, enemy spawn properly. However, after been built and run, the new client doens't show any enemy. I am sure that enemies exist on the server because they can be shot from the second client (even without seeing them).

Now if I try to host the server from the built and run version, nothing happens, and enemy are not present on any of the two clients. Furthermore, I can't even acces the console to see if the "OnStartServer" fonctions works properly as there is no console in the built version...

I created my 'enemy' prefab from the 'player' one, and the player works well in both clients. Those prefabs have a Network Identity, (+ Network Transform for the player), and are registred as 'spawn' in my network manager. The player is created each time a new client is opened. For the ennemy spawn, I have an object called 'Enemy Spawner', placed in my scene. It contains the previous code, and a Network Identity.

I have tryed to change the Netwotk Identities of the enemy prefab and the enemy spawner, leading to no real results.

Thanks for your time

Comment

People who like this

0 Show 0
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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

160 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can anyone help me with my network script? 1 Answer

Question regarding Server-Client approach in Unity 0 Answers

How to hide a started/full server 0 Answers

Photon - Lan? 1 Answer

Creating a multiplayer game. 1 Answer


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