- Home /
When I call a variable from another script, the value is from the last FixedUpdate, and not the current one. How would I fix this?
I have 2 different scripts. When the player's leaves a room, the camera pans over to the next room. A timer float variable is used in the calculations for the panning of the camera. The timer is in a FixedUpdate method. I have another FixedUpdate method on an enemy's script. When the player leaves the room the enemy is in, the enemy is destroyed. I was going to use the timer to destroy the enemy after the camera finished panning. The timer is a public static float and when set in the enemy's FixedUpdate method, is not the correct value. It appears to be the last value the timer was set to. I don't understand how Unity's sequence of events works within FixedUpdates or methods that have the same priorities.
Answer by Casiell · Aug 26, 2018 at 09:11 AM
Unity uses a Script Execution Order when dealing with events of the same priorities. I see two solutions for your problem. You can set execution order of your script to be always called last or you can create an event at the end of your camera movement. This way when your camera finishes you will just invoke the event and destroy the enemy.
You could also play around with coroutines, but I would need to see your code in order to suggest some solution.
Thanks for answering! Here are 2 of my scripts. $$anonymous$$y apologies if the code is messy, I need to go back and clean up all my scripts. The first method is is within the camera's FixedUpdate, void $$anonymous$$ovement (float speed) { controllerPosition = FindObjectOfType<TopDownController> ().GetControllerWorldPosition (); int horizontalRound2 = (int)(($$anonymous$$athf.Round (controllerPosition.x / (float)grid[0])) * (float)grid[0]); int verticalRound2 = (int)(($$anonymous$$athf.Round (controllerPosition.y / (float)grid[1])) * (float)grid[1]); if (horizontalRound2 != horizontalRound || verticalRound2 != verticalRound) { cameraReposition = new Vector2 (horizontalRound2 - horizontalRound, verticalRound2 - verticalRound); controllerReposition = cameraReposition / 2; distance = cameraReposition.magnitude; timer = distance / speed; controllerTimer = timer; horizontalRound = horizontalRound2; verticalRound = verticalRound2; cameraPosition = new Vector3 (horizontalRound, verticalRound, -10); stop = true; } transform.position = Vector3.$$anonymous$$oveTowards (transform.position, cameraPosition, speed * Time.fixedDeltaTime); if (timer > 0) { timer = timer - Time.fixedDeltaTime; } else { stop = false; } }
and the second method is the manager's FixedUpdate, void FixedUpdate () { topDownControllerPosition = FindObjectOfType<TopDownController> ().GetControllerWorldPosition (); topdownControllerHorizontalRound = (int)($$anonymous$$athf.Round ((topDownControllerPosition.x / (float)grid[0])) * (float)grid[0]); topdownControllerVerticalRound = (int)($$anonymous$$athf.Round ((topDownControllerPosition.y / (float)grid[1])) * (float)grid[1]); if (horizontalRound == topdownControllerHorizontalRound && verticalRound == topdownControllerVerticalRound && spawn == false) { spawnTopDownEnemy = Instantiate (topDownEnemy, topDownEnemy$$anonymous$$anagerPosition, Quaternion.identity); spawn = true; } else if ((horizontalRound != topdownControllerHorizontalRound || verticalRound != topdownControllerVerticalRound) && spawn == true) { topDownControllerTimer = TopDownCamera.timer; Debug.Log (topDownControllerTimer); Destroy (spawnTopDownEnemy, topDownControllerTimer); spawn = false; } }
In my case, when the player's rounded position is not equal to the camera's rounded position, the camera pans to a new position. The rounded positions create boundaries for rooms. The second script I've been having trouble with spawns an enemy when the player enters a new room, and deletes the enemy when the player exits the room. When trying to not have the enemy immediately destroyed upon exiting a room, I ran into my issue. All of my scripts currently use FixedUpdate exclusively, and communicate back and forth between each other. Do you think it would be better to rewrite my scripts to work with the script execution order settings, or rewrite my scripts with better coding conventions. I've done a little research and see how Coroutines could possibly work, but what do you think is the best for something like this?
I'm new to Unity Answers, and from the looks of it my code isn't readable on the website for me at least, so I took some pictures.alt text