Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Wagnerok · Aug 26, 2018 at 04:56 AM · variablesfixedupdatesequence

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wagnerok · Aug 27, 2018 at 05:42 AM 0
Share

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?

avatar image Wagnerok · Aug 27, 2018 at 05:46 AM 0
Share

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 alt text

f89fc0939101299a7911a39c0b27855e.png (21.8 kB)
f40426b46e9398387b4a919fae330eba.png (23.8 kB)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

91 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Approach to the command sequence execution 1 Answer

Variables Not Updating? 1 Answer

Physics behaviour changing with framerate - Is FixedUpdate() actually working properly? 3 Answers

Intermittent detection of input in FixedUpdate() 2 Answers

error CS1031: Type expected fix 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges