- Home /
Callback function cannot change game object status,callback function cannot change gameobject status
I used Redis database with the function sub/pub to get the data from database.
For the subscribe function, my code is like that:
sub.Subscribe("1", (channel, message) =>
{
Handler(message);
});
I put a "Handler" as a callback function to receive the publish information.
In the "Hanlder" function, I wish that I can receive some x, y, z position information and put into some game object in my Unity projcet.
private static void Handler(string message)
{
CarInfo temp = JsonConvert.DeserializeObject<CarInfo>(message);
Debug.Log(temp.wr);
float step = (float)temp.speed * Time.deltaTime;
Vector3 position = new Vector3((float)temp.xp + 5.0f, (float)temp.yp, (float)temp.zp);
Quaternion rotation = new Quaternion((float)temp.xr, (float)temp.yr, (float)temp.zr, (float)temp.wr);
GameObject.Find("Car2").transform.rotation = Quaternion.Slerp(GameObject.Find("Car2").transform.rotation, rotation, 0.1f);
GameObject.Find("Car2").transform.position = Vector3.MoveTowards(GameObject.Find("Car2").transform.position, position, step);
}
However, it seems like I can not change any information of a game object in the callback function.
Does anyone know how can I do for this?
Appreciate!!
You can only manipulate GameObjects on the main thread, iirc. What sort of concurrency is your setup using to get its messages?
Did you mean the parameter of string message from Handler pass value?
This string message is from Redis publish function. I publish this parameter when the FixedUpdate() being called.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
What is the best way to instatiate an array of connected GameObjects? 0 Answers
Object reference not set to an instance of an object 2 Answers
How to store player model and then swap the model to an object I Raycast hit? 0 Answers