- Home /
OnConnectedToServer, OnPlayerConnected, OnPlayerDisconnected not beeing called on instantiated prefab
Hi, I am having an issue with the network events above mentioned.
I have a mainSystem prefab, this prefab gets instantiated when the game starts.
This mainSystem prefab has a lot of custom components, and one of them is a customized networkManager( monobehaviour script)component. This component has one serialized gameobject public parameter, which I set in the unity editor to the reference of another gameobject prefab "NetModule" that has a networkView and a custom monobehaviour component on it.
The NetModule monobehaviour script has the event funcs "OnConnectedToServer" and so on on it. The NetModule prefab gets instantiated inside the networkManager using the Instantiate func, and the NetModule monobehaviour Start() func gets called so I assume that the Monobehaviour is correctly set up...
That is until I try to connect to another instance of the game ( Local or LAN )and expect that the OnConnectedToServer() gets called but nothing happens.
The strange thing is, if I put that same prefab on the scene using the editor, then that gameobject perfectly catches the OnConnectedToServer() event, so the connection procedure is fine as far as I can tell.
Also, if I declare the event funcs on the networkManager monobehaviour, the events get called without problem.
The basic source code would be something like this:
// this is attached to the mainsystem prefab
public class NetworkManager : MonoBehaviour{
public GameObject m_NetModule;
...
void Start(){
GameObject.Instantiate(m_NetModule);
}
...
}
// this is attached to another GameObject prefab that is dragged into the networkManager as m_NetModule
public class NetModule : MonoBehaviour{
void Start(){
// this gets called when the network manager calls: GameObject.Instantiate(m_NetModule)
}
void OnConnectedToServer(){
Debug.Log("Doesn't get called even if connected correctly to server.")
}
}
Any advice would be very appreciated!