Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Variable sometimes is 0, when trying to put time in the system.

Discussion in 'Scripting' started by Micio_del_Cheshire, Feb 19, 2019.

  1. Micio_del_Cheshire

    Micio_del_Cheshire

    Joined:
    Oct 24, 2013
    Posts:
    28
    I have some objects with this script on them, that make them scale up when the mouse is over them
    Code (CSharp):
    1. public void ZoomIn()
    2.     {
    3.         savedPosition = this.transform.position;
    4.         this.transform.position = new Vector3(savedPosition.x, savedPosition.y + 120f, savedPosition.z);
    5.         this.transform.localScale = new Vector3(2f, 2f, 1f);
    6.     }
    7.  
    8.     public void ZoomOut()
    9.     {
    10.         this.transform.position = savedPosition;
    11.         this.transform.localScale = Vector3.one;
    12.     }
    ZoomIn and ZoomOut are called by events.
    This works just fine. But I wanted to wait a bit, before the scale triggers, so I modified the game like this:

    Code (CSharp):
    1.  [SerializeField]
    2.     private float zoomDelay;
    3.     private float timer;
    4.     private bool isTimeCounting = false;
    5.  
    6.     void Update()
    7.     {
    8.         if (isTimeCounting)
    9.         {
    10.             timer += Time.deltaTime;
    11.  
    12.             if (timer >= zoomDelay)
    13.             {
    14.                 Zoom();
    15.             }
    16.         }
    17.  
    18.     }
    19.  
    20.     public void ZoomIn()
    21.     {
    22.         timer = 0f;
    23.         isTimeCounting = true;
    24.     }
    25.  
    26.     private void Zoom()
    27.     {
    28.             savedPosition = this.transform.position;
    29.             this.transform.position = new Vector3(savedPosition.x, savedPosition.y + 120f, savedPosition.z);
    30.             this.transform.localScale = new Vector3(2f, 2f, 1f);
    31.        
    32.             isTimeCounting = false;
    33.     }
    34.  
    35.     public void ZoomOut()
    36.     {
    37.         this.transform.position = savedPosition;
    38.         this.transform.localScale = Vector3.one;
    39.         isTimeCounting = false;
    40.     }
    Now, when the ZoomIn is called, a timer starts. If the zoomOut is called before the Zoom, the timer is stopped.

    This system works. Well, not really. Most of the time it works as intended. Sometimes there is an object that Zooms in regularly (saving the savedPosition variable), but when it's time to ZoomOut, the savedPosition becomes the Vector3.zero, moving the gameObject far away.

    I don't use those variables elsewhere.
    What am I doing wrong?

    Thanks for the answers.
     
  2. Micio_del_Cheshire

    Micio_del_Cheshire

    Joined:
    Oct 24, 2013
    Posts:
    28
    Nevermind. Writing the question, helped me finding the answer.

    I already tried avoiding using savedPosition while it could be uninitialized, in the ZoomOut function:

    Code (CSharp):
    1. if (savedPosition != null)
    2.       this.transform.position = savedPosition;
    but that wasn't the point. The gameObject wasn't being translated in the void, was translated to (0,0,0)

    With
    Code (CSharp):
    1. if (savedPosition != Vector3.zero)
    2.       this.transform.position = savedPosition;
    now works as intended. Sorry for the post, but I'm still thankful for possible comments if there are better ways to do this.