Search Unity

Fixed Update Not Consistently Executing [Code Posted]

Discussion in 'Scripting' started by ShokWayve, Feb 14, 2020 at 2:46 PM.

  1. ShokWayve

    ShokWayve

    Joined:
    Jan 16, 2013
    Posts:
    6
    Greetings Good People.

    I have an app wherein I have 4 cubes in 4 different slots. When the user drags one cube from its slot, once the user lets go of the cube, it should find the closest slot, go to it, and if there is another cube already in that slot, it should go to the slot that was just emptied by the incoming cube. In other words, the cubes should swap positions.

    My code implements this by checking in Update, whether the user has started the touch process (the final version on the device) or whether the OnMouseDrag (overridden as its own method) is called (on the computer for testing). Once the drag process starts, then flags are set (e.g. doSwap = true so that a function called in Fixed Update will execute its code since the doSwap bool will equal true when it checks it), and methods are called in FixedUpdate to implement the process. My problem is that sometimes it works, sometimes it doesn't. While I can always move the cube and it will go to and find the closest slot, sometimes it will swap locations with a cube already there, and sometimes both cubes stay there.

    Any ideas on how I can fix this? Is there a way to get this to run consistently each time? I wonder if it is a timing issue of some kind since it works sometimes but not all the time. So I wonder if there is a better way than a flags based system where functions run in Fixed Update if certain boolean values/conditions are met, and then stop running when those conditions are not true.

    Thanks.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,971
    I'd suggest you show your code using code tags and people can help you. The description is good, but better to see what you're doing. But also, FixedUpdate is usually for physics and if you're changing a bool in Update, it may not be changed when you want and that is why it doesn't execute.

    https://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html
     
  3. ShokWayve

    ShokWayve

    Joined:
    Jan 16, 2013
    Posts:
    6
    Thanks for your reply. The bools are being changed in methods that are in FixedUpdate. Update changes the first bool which starts the process.

    I will try to post the code during my next break.

    Is this flags/bools based approach good though? I am wondering if there is another way to synch a sequence of calls in Fixed Update on multiple game objects. Sometimes, I have found that different parts of the code may be executing more quickly and thus unexpected behavior may occur.
     
  4. VishwasGagrani

    VishwasGagrani

    Joined:
    May 12, 2018
    Posts:
    21
    Is it possible for you to share the part of the code that is causing the problem? So that I can reproduce at my side an analyse?
     
  5. ShokWayve

    ShokWayve

    Joined:
    Jan 16, 2013
    Posts:
    6
    Thanks. I will post it during lunch.
     
  6. ShokWayve

    ShokWayve

    Joined:
    Jan 16, 2013
    Posts:
    6
    Greetings. I got it to work. The problem was the condition for how close they had to be was too small so when it arrived, depending on when Fixed Update was running, it wasn't close enough to trigger the swap process.

    Everything is working now. Thanks everone.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    4,983
unityunity