Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Moving Something separate times with one script

Discussion in 'Scripting' started by BlizzWizz, Jul 31, 2023.

  1. BlizzWizz

    BlizzWizz

    Joined:
    Jul 5, 2023
    Posts:
    3
    I am making a 2D game and trying to make two buttons move out of frame when the start button is pressed as the reset button. I also use this script at the beginning of the game to first start off. But when you press the start game button as the reset button the buttons don't move out of frame.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MoBuOOF : MonoBehaviour
    6. {
    7.     private float move = 0.03f;
    8.     private float time = 0;
    9.  
    10.     // Update is called once per frame
    11.  
    12.     private void Start()
    13.     {
    14.         time = 0;
    15.         move = 0.03f;
    16.     }
    17.     void FixedUpdate()
    18.     {
    19.         time += Time.deltaTime;
    20.  
    21.         if (1.5 < time)
    22.         {
    23.             if (0.12f > move)
    24.             {
    25.  
    26.                 transform.Translate(Vector2.down * move);
    27.                 move += 0.001f;
    28.             }
    29.         }
    30.     }
    31. }
     
  2. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    For one you're using FixedUpdate() and calling Time.deltaTime, which is a Update() reference. Time.fixedDeltaTime is used for FixedUpdate(), but that is usually used for physics based calls.

    I would try and debug this first:
    Code (CSharp):
    1. public class MoBuOOF : MonoBehaviour
    2. {
    3.     private float move = 0.03f;
    4.     private float time = 0;
    5.     public bool test;
    6.    
    7.     void Update()
    8.     {
    9.         if (test)
    10.         {
    11.             time += Time.deltaTime;
    12.    
    13.             if (time < 1.5f) // was backwards
    14.             {
    15.                 if (0.12f > move)
    16.                 {
    17.                     transform.Translate(Vector2.down * move);
    18.                     move += 0.001f;
    19.                 }
    20.             }
    21.         }
    22.     }
    23. }
    And make sure all your time references work as intended. You would have to select said object in the hierarchy, and click it's "test" boolean when you are ready to watch it move, and it's transforms positions or other variables.

    Also would recommend moving said object just till it reaches a position out of screen, not by time. But if you are enabling and disabling this gameObject? you will need:

    Code (CSharp):
    1. private void OnEnable()
    2.     {
    3.         time = 0;
    4.     }
    5.     private void OnDisable()
    6.     {
    7.         // if needed
    8.     }
     
    BlizzWizz likes this.
  3. BlizzWizz

    BlizzWizz

    Joined:
    Jul 5, 2023
    Posts:
    3
    Hey, thank you for helping. I am also now going to change the movement to a sett location.
     
  4. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    ..and when comparing values with variables always place the variable on the left like this:

    if (x>5)

    This way when you have problems and post your scripts on the forums you won't cause old programmers to have a brain seizure.:eek:
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Why are you coding all this movement?

    Just make a simple canned animation and trigger it when needed.

    Another approach would be to use a tweening package such as LeanTween, DOTween or iTween.

    While you are animating UI stuff, make sure you get the anchoring / scaling correct or you'll end up redoing it again and again.

    Here are some notes on UI Anchoring, Scaling, CanvasScaler, etc:

    https://forum.unity.com/threads/inc...size-between-two-people.1130146/#post-7261747

    https://forum.unity.com/threads/game-ui-button-size-problem.1142650/#post-7337383

    Usually you need to choose a suitable ScaleMode and MatchMode in the Canvas Scaler and stick with it 100%. Generally if you change those settings you will often need to redo your UI entirely.

    I also use this
    CanvasScalerOrientationDriver
    utility to make sharing UI for Landscape / Portrait easier. Read what it does carefully.

    https://gist.github.com/kurtdekker/8802b1b6c708637398f8c9167641efd3
     
  6. BlizzWizz

    BlizzWizz

    Joined:
    Jul 5, 2023
    Posts:
    3
    I did manage to find a much better script that i can use but for some reason I don't get my public void OnClick in the button OnClick menu upload_2023-8-2_9-47-22.png

    Code (CSharp):
    1. public class MoveInn : MonoBehaviour
    2. {
    3.     public float speed = 4f;
    4.     public Vector2 _targetPosition;
    5.  
    6.     public void Start()
    7.     {
    8.         _targetPosition = transform.position;
    9.     }
    10.  
    11.     public void OnClick(Vector2 position)
    12.     {
    13.         _targetPosition = position;
    14.     }
    15.  
    16.  
    17.     public void Update()
    18.     {
    19.         transform.position = Vector2.MoveTowards(transform.position, _targetPosition, speed * Time.deltaTime);
    20.     }
    21.  
    22.  
    23. }
     
  7. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    I had a similar issue, when playing with buttons. There is more to that second small window pop-up, and 2 sets of functions are separated by a line. Sometimes the function you want is hiding in one of them, so are you able to show a picture of the lower section of that second window? beneath said line?
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Unity button onclick function script callback gameobject notes:

    https://forum.unity.com/threads/onclick-no-functions-shown.669334/#post-7328488

    For sliders and dropdowns and other value-returning UI elements:

    https://forum.unity.com/threads/cant-return-a-value-for-drop-down-list.1171442/#post-7504738

    And passing "more interesting" things when buttons are pressed:

    https://forum.unity.com/threads/unityui-button-auto-selected-when-scene-play.1289603/#post-8175986