I use Unity 5.3.4 with C#.
I have created a global socket system that allows me to stay connected during all the game. The script (ClientManager) is placed in an indestructible GameObject (with DontDestroy). It is initialsé was the first scene.
Only, I will need to find a GameObject when I am in another scene.
The problem is that my script is not execute on the main thread. So I can not. I can not execute my search in Start()
because I'm not in the right scene and the desired GameObject is not present.
I would need to create a common évennements different scripts in all the scenes, or then execute my GameObject.Find ()
in the main thread.
I created a system of listener. I could also solve my problem by adding a listener to the script searched ...
Do you know how to do this? Do you have another solution?
Here are my scripts:
Client Manager :
public class ClientManager : MonoBehaviour, ClientListener {
private Client client;
public Client Client {
get { return client; }
}
void Start () {
this.client = new Client (this);
}
// ...
public void OnFriendDisconnected(string username, int id){
//...
}
public void OnFriendConnected(string username, int id){
//...
GameObject gm = GameObject.Find ("r_Slide_view");
if (gm != null)
gm.GetComponent<rslideController>().IsOnline(username);
}
public void OnReceiveMessage(string message){
//...
}
public void OnAuthenticated(){
//...
}
public void OnRejected(){
//...
}
public void OnDisconnected(){
//...
}
void OnLevelWasLoaded(int level) {
GameObject gm = GameObject.Find ("r_Slide_view"); // ERROR - Is not execute in the main thread
if (gm != null)
SlideController = gm.GetComponent<rslideController>();
}
}
Listener :
public interface ClientListener {
void OnAuthenticated();
void OnRejected();
void OnDisconnected();
void OnFriendDisconnected(string username, int id);
void OnFriendConnected(string username, int id);
void OnReceiveMessage(string message);
}
rSlideController (searched script ):
public class rslideController : MonoBehaviour {
// How to know when the methods are called?
}