Search Unity

Hierarchy affecting how game reacts.

Discussion in 'Editor & General Support' started by pboy227, Mar 19, 2020.

  1. pboy227

    pboy227

    Joined:
    Mar 19, 2020
    Posts:
    2
    Unity reacts differently depending on which item is chosen in the hierarchy while running my game.

    In my example, I have a rocket ship that moves slow and no particle effect while it is selected in the hierarchy however, whenever anything besides the rocket ship is selected, the rocket ship moves faster and the particle effect works as intended. See my youtube video to see what I'm talking about.



    I have uninstalled and reinstalled. I even downloaded the entire project from my instructor and ran that with the exact same results which are not seen in the instructors videos. The instructor is using an older version of Unity so there may be some issue there but I don't see why the effect is triggered by hierarchy selection.

    I haven't been able to find anything about this but it is driving me insane and is making me want to give up on learning Unity since I don't know what effect my code and changes are actually having.
     
    EffYouCeeKay likes this.
  2. pboy227

    pboy227

    Joined:
    Mar 19, 2020
    Posts:
    2
    Anyone? This is frustrating and I feel like I can't make any progress until this is resolved.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,692
    Aw, don't do that! Engineering is like puzzle solving sometimes.

    Here's some random ideas to try:

    - look for errors at runtime as well as warnings

    - what if you select the rocket and shift-select something else? What about if you press ctrl-A in the hierarchy window? What happens? can you learn anything from it?

    - right click on the inspector window, open a second inspector window, then select the rocket and LOCK (little icon in the upper left of the window) that inspector window to show the rocket always. Does that fix it? Is there any info to be gained by how it changes behavior?

    - either put a breakpoint on the code that enables the particle system, see if it fires when you thrust, or alternately do a Debug.Log() to show that you pressed and released the key. Is there any change from selected to not?

    - notably I already see the problem appears to not affect success particles... have you tried remaking the jet particles and reconnecting them to wherever they are dragged into the player controller script?

    - look for functions such as OnDrawGizmosSelected and see if there is inadvertently some critical logic being executed in there, such as enabling or setting a variable. That's an easy and common mistake.

    - what happens when you build it to an actual executable? Obviously there you can't select the rocket, so it's gonna either succeed or fail.

    The list is endless, but you just sorta press ahead, chop away parts of the problem, bisect it, look at it from different angles, and eventually you WILL figure out what is going wrong, and you WILL feel smarter.

    Remember the six steps of debugging:

    During debugging, developers and programmers go through a cycle of emotional states:

    DENIAL. That can't happen.
    FRUSTRATION. That doesn't happen on my machine.
    DISBELIEF. ...
    TESTING. ...
    GOTCHA. ...
    RELIEF.

    Don't get hung up on step 2 or 3, move onto TESTING and eventually you will reach GOTCHA.

    Best part is, take careful notes of what you tried, then you have an awesome story of victory to tell your professor.
     
    Joe-Censored likes this.
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Add debugging
    then add more debugging
    then add more debugging

    You should put Debug.Log statements everywhere involved in the process of moving the rocket and activating the particle system. Most likely you'll see a difference in the output between the two scenarios which will lead you towards the cause.
     
    Kurt-Dekker likes this.
  5. EffYouCeeKay

    EffYouCeeKay

    Joined:
    Apr 2, 2020
    Posts:
    1
    I'm having the same issue.

    It has nothing to do with game-specific code, as other comments suggest.

    My objects move at different speed, and the camera is jittery, when specific objects are selected in the Hierarchy.

    Hopefully someone experienced figures out the problem for us beginners soon!
     
  6. lejean

    lejean

    Joined:
    Jul 4, 2013
    Posts:
    392
    I got the same, it's just something in unity I guess not really an error in your code.
    When I select my character while playing the game's FPS drops significantly.
    I guess it got something to do with the amount of components on the gameobject or something like that.
     
  7. Kich867

    Kich867

    Joined:
    Jan 12, 2018
    Posts:
    14
    If its any consolation, I'm following the same Unity course you are, making this game right now, and am experiencing identical behavior as shown in your video. I don't understand how having something selected in the hierarchy affects its behavior.
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,692
    You do see the Inspector window updating in Unity, right?

    I'll give you 3 guesses which CPU is doing that work.
     
  9. Kich867

    Kich867

    Joined:
    Jan 12, 2018
    Posts:
    14
    Is the implication here I need to reduce the force and something else for the particle system? In the scene view, outside of playing, the particle system works fine. It is entirely non-functioning during the actual play-mode though if I don't have the Rocket selected (as shown in this person's youtube video).

    So, I'm sorry I genuinely don't quite follow your vague question and ask. Why does the particle system operate as expected in the scene view, work as expected while the object is selected in the hierarchy (which I think you're implying its slowing the object down by having to display those updates?), but not work when its not selected and doesn't have to show those updates?

    Do you know what the issue is and you're not being explicit about it or? Could you just explain it? To be clear, we're following a Udemy course in which these particle effects and scripts are either provided or you follow along as you go, the whole point is that I don't know what I'm doing and I'm trying to learn.

    In the course, the person demonstrating in the video never has their rocket selected in the hierarchy and it works exactly as expected. I can only achieve that if I have the rocket selected, otherwise it operates differently.

    *EDIT* -- Discovered the issue. Turns out its because it was calling .Play() in the update loop. You can prevent this by only calling .play() if "isPlaying" is true. It's still unclear to me why selecting the object in the Hierarchy made this work, as even slowing it down it'd be constantly overwriting itself spamming play().

    However, could you elaborate, does this mean that the current implementation of this movement is frame-rate dependent? I'm currently running this scene at 3000fps, it drops down to about 2200fps if I have it selected while trying to move and it moves substantially slower.
     
    Last edited: Aug 2, 2020
    PraetorBlue likes this.
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,692
    I apologize for not being more direct: I was simply observing that Unity UI is updated on the same thread as your game in the editor.

    This means that if you select a GameObject with a complex editor window, the inspector window must show it... every frame.

    Now if this inspector window is full of all kinds of fitsy-futsy parts (and ParticleSystems have a LOT of freakin knobs and whistles), as do AudioSources, that's a LOT of extra CPU not available for your game.

    We also all agree that OnGUI() (which is now the Unity editor updates) is not very performant.

    As for the specific Play() issue, it perhaps is harmless enough to call Play each frame as long as there is no inspector to update, but when there is, it might be a very computationally expensive hit.
     
  11. MurrSquadInteractive

    MurrSquadInteractive

    Joined:
    Apr 4, 2019
    Posts:
    1
    MASSIVE NECRO HERE, but after following the advice of @Kurt-Dekker I was able to narrow down my own issues with hierarchy-dependent performance dips to my Player Input component being open.

    My Player GameObject is covered in little components, but by checking the Profiler with each component pulled open, one at a time, I found the Player Input component to pretty much be the sole culprit here.

    Even if you are experiencing this without a Player Input component present, try this approach to troubleshooting this issue. I hadn't previously considered Unity's OnGUI update as part of the performance of my game in the editor, but you learn something new every day.

    upload_2023-3-18_14-49-41.png

    upload_2023-3-18_14-50-21.png
     
  12. Reswin_RS

    Reswin_RS

    Joined:
    Jan 27, 2022
    Posts:
    1
    Hi i am also facing same issue how to fix it ?
     
  13. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,692
    Probably not.


    Please don't necro-post. If you have a new question, make a new post. It's FREE!!




    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?



    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    - Do not TALK about code without posting it.
    - Do NOT post unformatted code.
    - Do NOT retype code. Use copy/paste properly using code tags.
    - Do NOT post screenshots of code.
    - Do NOT post photographs of code.
    - ONLY post the relevant code, and then refer to it in your discussion.
     
  14. wingsneon

    wingsneon

    Joined:
    Jul 5, 2018
    Posts:
    23
    I found the solution for my project. I was having the same issue. Depending on the gameobject I select in the hierarchy, the performance seemed to change, reading through this post I was able to find out that this is related to the various components being executed at the same time. I still don't know if this is valid only for the editor (probably yes, cause in a builded game you wouldn't be able to select anything from the hierarchy that would cause abrupt performance changes).

    Moving on, my object was moving way more faster when I selected the player gameobject, it was also causing the fps to go down... so the object was moving faster when the fps was lower, but moving slower when the fps was higher... There is the problem. I capped the fps using
    Application.targetFrameRate = 60;
    inside of
     void Start()
    and it fixed, now it moves the same no matter which object I select.

    Problems with this solution:
    1. If I want more fps, I'll have to modify the amount that the object moves, so it moves the appropriate distance that I want.
    2. Even capped at 60fps, variations or even performance issues with someone else's device can cause the movement to be different.
    3. Wether my game is 2d, it is not pixelated - if it was pixelated, I wouldn't mind to cap the fps at let's say 30. But that's not the case, I have some fluid 2d animations which would looks way better at higher FPS. So there might be a better solution to this problem.
    That being said, right now I'm gonna search more about Time.deltaTime, (This is the responsible for all this trouble - at least in my case (please corrent me if I'm wrong!)) - and the problem is that I only know this way of dealing with time through FPS.


    [EDIT]
    So I fixed the whole problem by inserting the code (actually a function) inside of
    [B]void FixedUpdate()[/B]
    instead of
    void Update()
    . FixedUpdate is not limited to be called everyframe, instead, its called as many times as it will be needed for the physics to be applied correctly to something.
    In my case, running an method to move an object in void Update() was causing the object to move differently depending on the framerate (if the frame rate was 150fps, then it would be called 150 times per second). With FixedUpdate(), I don't know how many times it's being called but, it's moving the gameobjects just the way I wanted.
     
    Last edited: Jan 27, 2024
  15. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,433
    Did you read the part above where it's time to put this topic to rest? Don't necropost.

    In the Editor, your game performance will be worse than in a standalone build. Period. If you scrunch the Hierarchy down, and you select no objects so the Inspector is empty, it will get a lot better performance than if you have a Hierarchy filled with objects and an Inspector showing all kinds of values changing. On a slower machine, the Editor will chug along so weakly that you will see all kinds of quirks in the way your code runs, because the time interval is larger and/or the game time is falling behind the real clock scheduler's goals. That has NOTHING to do with how
    Time.deltaTime
    works technically, or how fast the game will run in a standalone build.
     
  16. wingsneon

    wingsneon

    Joined:
    Jul 5, 2018
    Posts:
    23
    Thanks for letting me know that.

    About this being an necropost: I replied the post with helpful information, I don't care if it's being ressurected. I was having the SAME problem as OP, and no answer here seems to clarify what the problem was. It wouldn't make sense to create a new post because I already solved my problem. I was trying to help people that come here with the same issue.
    When someone has a problem, they don't go imediately to the forum. Most people, just like me, they just search in Google and open the first results that come up, and get into old posts like this.
     
    Last edited: Feb 2, 2024
    tsukimi likes this.
  17. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,433
    You replied with conjecture, which turned out to be wrong. Necro-posting is against the rules of the forum, and it wastes the time and efforts of people who moderate the forum, because old threads have old issues that often don't even match the versions or issues that newer versions of Unity have.
     
  18. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 5, 2024
    Posts:
    472
    Unless you move your objects with physics doing it in
    FixedUpdate
    is a mistake. If you do not move them with physics, you just need to multiply your movement delta with
    Time.deltaTime
    . So it will move proportionally to deltaTime.