Search Unity

Object behaving differently in Play mode depending on what's selected

Discussion in 'Getting Started' started by jjguitarist, Dec 31, 2022.

  1. jjguitarist

    jjguitarist

    Joined:
    Aug 2, 2020
    Posts:
    3
    Hi all,

    I'm going through the 'Essentials of programming in Unity' section of the Unity Essentials learning module, and have reached the point of adding a script to a ball to cause it to grow in size each frame (within my 'The Floor is Lava' scene).

    I've noticed some odd behaviour when running the game. If I run the game with the ball Object selected in the Hierarchy/Scene, it'll follow a particular path across my other Objects. However, if I run it with any other Object selected, it follows a different path. In both cases the ball grows as expected. Before adding the script, the ball would always follow the same path regardless of which Object was highlighted (as I'd expect).

    I'm not looking to solve this, I'm just hoping someone will be able to explain why this is happening? If this behaviour is replicated in other projects I can see it becoming a more serious issue. Cheers.
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,060
    Could you maybe make a recording of this? I don't quite follow
     
  3. jjguitarist

    jjguitarist

    Joined:
    Aug 2, 2020
    Posts:
    3
    Hi DevDunk, I think I sort of understand what's happening now. Since reopening the project, every time I run the game the ball is now behaving slightly differently, as opposed to before when it seemed to run one way when the ball Object was selected and another when any other Object/no Object was selected.

    I assume every time the transform.localScale script runs it's running slightly differently e.g. one time it increases the X variable before the others, the next time it increases Y first, the next time Z etc. Does that sound correct? Or should it be behaving the exact same way every time?
     
  4. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,060
    Depends on your script. Share it
     
  5. Number-3434

    Number-3434

    Joined:
    Dec 31, 2022
    Posts:
    13
    It may be down to floating point errors.
     
    jjguitarist likes this.
  6. jjguitarist

    jjguitarist

    Joined:
    Aug 2, 2020
    Posts:
    3
    Hi DevDunk, the script is:

    Code (CSharp):
    1. public class BallTransform : MonoBehaviour
    2. {
    3.     public Vector3 scaleChange;
    4.     // Start is called before the first frame update
    5.     void Start()
    6.     {
    7.      
    8.     }
    9.     // Update is called once per frame
    10.     void Update()
    11.     {
    12.         transform.localScale += scaleChange;
    13.     }
    14. }
    And each Scale Change variable was set to 0.0001 in the Inspector.

    Oh interesting, having now briefly read about this I think you're probably right. I'm essentially a complete novice when it comes to programming so wasn't aware of this as a potential issue.

    So, to check my understanding, the different behaviour of the ball each time I run the game is possibly being caused by how binary systems handle floating point numbers?
     
    Last edited: Jan 1, 2023
  7. AdrielCodeops

    AdrielCodeops

    Joined:
    Jul 25, 2017
    Posts:
    54
    Maybe the selection of one or another component decreases your framerate and Update is not frame independent so the speed of ball growth could vary between runs. Try using FixedUpdate instead of Update.
     
    jjguitarist likes this.
  8. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    CODE tags are your friend :D

    Happy New Year!
     
    jjguitarist likes this.