Why i can't create a GameObject using my own function? I'm using nodejs as a server and unity as a client. My problem comes when I want to change the scene or do anything where I don't want to go through an update, for example:
if (comando.Contains("spawn")) // Si recibe el mensaje del servidor como 'conectado' hara el debug
{
string[] player = comando.Replace("spawn", "").Replace("{", "").Replace("}", "").Replace("\"", "").Replace("username", "").Replace("id", "").Replace(":", "").Split(',');
GameObject go = new GameObject("Server ID:" + id);
go.transform.SetParent(container);
serverObjects.Add(id, go);
}
This is a message that is sent from the server, once it gets to the client it tries to create a GameObject with a name "x" , the "error" comes here, if I execute this code, it gets to string[] player but it doesn't execute the
GameObject go = new GameObject("Server ID:" + id);
but if I put this code instead:
if (comando.Contains("spawn")) // Si recibe el mensaje del servidor como 'conectado' hara el debug
{
string[] player = comando.Replace("spawn", "").Replace("{", "").Replace("}", "").Replace("\"", "").Replace("username", "").Replace("id", "").Replace(":", "").Split(',');
id = player[1];
crear = true;
}
and this in the update function
if (crear)
{
if (serverObjects.ContainsKey(id))
{
crear = false;
}
else
{
GameObject go = new GameObject("Server ID:" + id);
go.transform.SetParent(container);
serverObjects.Add(id, go);
crear = false;
}
}
then it does work, but I don't want to have to create a global variable to store the id and a bool for do some shit, since it is used by every person who enters.
Whatever it is, I'm not looking for you to fix it, I'd just like to understand why it works like this and why I can't make it work without going through the update, because for other things it does work.
And I'm going crazy, thanks a lot, sorry for my English :)
PS: more examples that don't work:
if (comando == "noconectado") // if server send me "noconectado" y change the scene
{
UnityEngine.SceneManagement.SceneManager.LoadScene(1);
}
but if I do this
if (comando == "noconectado") // if server send me "noconectado" y change the scene
{
creado=true;
//UnityEngine.SceneManagement.SceneManager.LoadScene(1);
}
and this in the update function
if(creado)
{
UnityEngine.SceneManagement.SceneManager.LoadScene("Player");
creado = false;
}
works.
In this code:
if (comando.Contains("spawn")) // Si recibe el mensaje del servidor como 'conectado' hara el debug
{
Debug.Log("1");
string[] player = comando.Replace("spawn", "").Replace("{", "").Replace("}", "").Replace("\"", "").Replace("username", "").Replace("id", "").Replace(":", "").Split(',');
Debug.Log("2");
id = player[1];
Debug.Log("3");
GameObject go = new GameObject("Server ID:" + id);
Debug.Log("4");
go.transform.SetParent(container);
Debug.Log("5");
serverObjects.Add(id, go);
}
this just return me 1 , 2 , 3
My client get the message like that
stream.BeginRead(data, 0, data.Length, new AsyncCallback(terminoDeLeer),
stream);
void terminoDeLeer(IAsyncResult arr)
{
leyendo = false;
int t = stream.EndRead(arr);
string mensaje = Encoding.UTF8.GetString(data, 0, t);
leerComando(mensaje);
}
and leerComando is what i show higher up
Im getting crazy
PS2: I get no errors, everything works if I put it in the update function, that's why I ask because according to my logic the two ways should work, but for what I need, I need to use the one that doesn't work and I don't understand why :(.
Thank you. why you vote negative? i dont understand, its a good question, and anyone can answer it so wtf is wrong with you guys?