- Home /
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
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.
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.
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.
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 ;)