Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by Zwusel · May 27, 2016 at 12:12 PM · multiplayerspawn

Unet: Where or when to spawn Environmental objects (OnStartServer does not work properly)

Hi there, i just wanted to ask if there is a Method where things like creating the environment normally are. At the moment i am doing it in the OnStartServer method but when i log out with the host and log back in the objects wont be spawned again. Which method is called when somebody clicks on LAN Host?

ty

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Wolfrik_Creations · May 27, 2016 at 09:33 PM

When someone hosts a server it runs the OnServerInitialized function, when someone joins a server it runs the OnConnectedToServer function.

I'm confused on what you're asking, if you're using a procedural world then try to Network.Instantiate all of your objects OnServerInitialized.

If it's not procedural and the objects don't change then you don't need to spawn them, they're already in the game.

Comment
Add comment · Show 8 · 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 Zwusel · May 29, 2016 at 05:36 PM 0
Share

Hi there, yea i am (just for testing purposes) spawning a procedural Asteroid Field.

Hm i tested the OnServerInitialized function but it doesnt get invoked :(

Here is my spawner class. It is on a empty GO inside the scene and has a Network Identity on it.

 using UnityEngine;
 using UnityEngine.Networking;
 
 public class AsteroidField$$anonymous$$anager : NetworkBehaviour
 {
     [SerializeField]
     private int asteroids$$anonymous$$ax = 100;
     [SerializeField]
     private float asteroid$$anonymous$$axSize = 3.0f;
     [SerializeField]
     private float asteroid$$anonymous$$inSize = 1.0f;
     [SerializeField]
     private float asteroid$$anonymous$$axSpeed = 1.0f;
     [SerializeField]
     private float asteroid$$anonymous$$inSpeed = 0.0f;
     [SerializeField]
     private float asteroidDistance = 10.0f;
     [SerializeField]
     private GameObject asteroid;
 
     private float asteroidSize;
     private float asteroid$$anonymous$$ass;
     private Vector3 randomPos;
 
 
     public override void OnStartServer()
     {
         //base.OnStartServer();
         Debug.Log("Server started");
         CalculateAsteroidField();
     }
 
 
     void OnServerInitialized()
     {
         Debug.Log("Server initialized and ready");
     }
 
 
     private void CalculateAsteroidField() 
     {
         for (int i = 0; i < asteroids$$anonymous$$ax; i++) 
         {
             asteroidSize = Random.Range(asteroid$$anonymous$$inSize, asteroid$$anonymous$$axSize);
             asteroid$$anonymous$$ass = asteroidSize * 10f;
 
             asteroid.transform.localScale = Vector3.one * asteroidSize;
             asteroid.GetComponent<Rigidbody>().mass = asteroid$$anonymous$$ass;
 
             randomPos = Random.insideUnitSphere * asteroidDistance;
             
             while(Utils.CheckOverlap(randomPos, asteroid.transform.localScale))
             {
                 randomPos = Random.insideUnitSphere * asteroidDistance;
             }
 
             GameObject asteroidInstance = (GameObject)Instantiate(asteroid, randomPos, Random.rotation);
 
             asteroidInstance.name = "Asteroid" + i;
 
             if (Random.Range(0, 3) == 0)
             {
                 float randSpeed = Random.Range(asteroid$$anonymous$$inSpeed, asteroid$$anonymous$$axSpeed) * 0.5f;
 
                 asteroidInstance.GetComponent<Rigidbody>().velocity = randSpeed * asteroidInstance.transform.forward;
             }
 
             NetworkServer.Spawn(asteroidInstance);
         }
     }
 }


It kinda works but only the first time i click on "LAN Host(H)". When is stop the host and click on "LAN Host(H)" again the OnStartServer fuction doesnt get called. And unforunately the OnServerInitialized fuction doesnt get called at all. Any suggestions? Oh we are of courese talking about the Network$$anonymous$$anager and its HUD.

avatar image Wolfrik_Creations Zwusel · May 29, 2016 at 06:21 PM 0
Share

Well, you could try changing "void OnServerInitalized" to "public void OnServerInitialized" but I don't prefer C# so I don't know how much of a difference that would make.

Also I'm pretty sure that those commands are not ran in the Unity 5 Unet system, I think they're only for legacy.

Is it possible to make Unet run a function when you click "LAN Host(H)"? Because if so you could just make a function called something like "ServerStart" and make it call it.

I apologize for not being able to help much, I just don't use Unet.

avatar image Zwusel Wolfrik_Creations · May 29, 2016 at 06:49 PM 0
Share

I dont know if it is possible to run a function when the button is pressed. I am still hoping there is a function called i can use for unet. Np i am thankful for any suggestion ;)

Show more comments

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to manually handle multiplayer prefabs spawning with our own script ? 0 Answers

MLAPI spawn player prefab 2 Answers

How to get players to re-spawn with a RPC. 0 Answers

Multiplayer prefab spawns with wrong orientation 1 Answer

mutiplayer player clone not active 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