Unity won't Instantiate() outside of Start(), even though code is the same
I am trying to add a gameobject to my game using a socket connection. I have a SocketHandler class, which simply connects to my NodeJS socket server, which can also call a method on another class, which will add a new gameobject to the game. Here is how it basically works:
void Start () {
GameObject car = Instantiate(CarGameObject);
car.transform.parent = this.transform;
car.GetComponent<TestCar>().text.text = "Volkswagen";
}
This works fine and it adds the gameobject to the game upon starting. However, if I do something like this in my SocketHandler, it doesn't work:
var addCar = new AddCar();
socketio.On("addCar", (data) => {
string str = data.ToString();
Car data2 = JsonConvert.DeserializeObject<Car>(str);
addCar.AddNewCar(data2);
});
and then inside my AddCar class, I have the AddNewCar() method:
public void AddNewCar(Car input) {
GameObject car = Instantiate(CarGameObject);
car.transform.parent = this.transform;
car.GetComponent<TestCar>().text.text = input.name;
}
it does not work. If I do a Debug.Log(input.name) inside the AddNewCar() method, it does log the name the server sends. It should do the exact same thing, but the AddNewCar() doesn't get added to the game. It simply does nothing.
What could be wrong?
Your answer
Follow this Question
Related Questions
Best way to use socket.io client with Unity 5 Personal 2 Answers
Need help with Socket Interactor 0 Answers
Spawn player prefab on sockets c# 0 Answers
Unity socket AsyncCallback delay 0 Answers
SSL over Socket hangs on AuthenticateAsClient step 0 Answers