0
\$\begingroup\$

I have three components of script which are attached to different objects

  1. DisplayMenu

  2. PlayAnimationOnMouseDown

  3. DisplayMachinePartName

Attached with the object and getting very strange behavior on run time. Whenever I run the game my script order become change. In editor it is same as we mentiond above but on on runtime it becomes

  1. PlayAnimationOnMouseDown
  2. DisplayMachinePartName
  3. DisplayMenu

Whereas on a different object it remains in the same order! I didn't change execution order in editor settings.

Edit: As comment suggested, it is better to not trust ordering of the script component. Therefore i am sharing my code that is dependent on order of the script:

Here is my Menu script that display menu based on enum state:

 /// <summary>
    /// It will shown the three button menu on mouse click.
    /// </summary>
    public class DisplayMenu : MonoBehaviour
    {
        public GameObject canvasMenuNavigation;
        public Image panelNavigation;
        public float machineNameYPosition = 0f;

        [SerializeField]
        BtnFurtherExplosionAnimationDeactive btnFurtherExplosionAnimationDeactive;

        public void Reset()
        {

            canvasMenuNavigation                    = GameObject.Find("Canvas_MenuNavigation");
            panelNavigation                         = canvasMenuNavigation.GetComponentInChildren<Image>();
            btnFurtherExplosionAnimationDeactive    = canvasMenuNavigation.GetComponentInChildren<BtnFurtherExplosionAnimationDeactive>();

        }

        private void OnMouseDown()
        {
            if (AnimationStateManager.currentAnimationState != AnimationStates.None)
            {
                Debug.Log("onmousedown");
                DisplayMenueCanvas(true);

                panelNavigation.transform.position = new Vector3(
                    this.transform.position.x,
                    this.transform.position.y + machineNameYPosition,
                    this.transform.position.z);


                string currentAnimatioType = this.name.Split('_')[3];
                Debug.Log(currentAnimatioType);

                AnimationStates selectedState = (AnimationStates)System.Enum.Parse(typeof(AnimationStates), currentAnimatioType, true);
                FindObjectOfType<SelectedAnimationType>().selectedAnimationType = selectedState;

                btnFurtherExplosionAnimationDeactive.ButtonActive();
            }

        }


        void DisplayMenueCanvas(bool isActive)
        {
            canvasMenuNavigation.SetActive(isActive);
        }
    }

And here is my script that play the animation:

/// <summary>
/// This will run the aniamation at first level.
/// </summary>
public class PlayAnimationOnMouseDown : MonoBehaviour
{
    public AnimationController animController;

    // Start is called before the first frame update
    void Reset()
    {
        animController = GameObject.Find("VBD_Turbine_Exploded_001").GetComponent<AnimationController>();
    }


    private void OnMouseDown()
    {

        if (!EventSystem.current.IsPointerOverGameObject())
        {
            if (AnimationStateManager.currentAnimationState == AnimationStates.None)
            {
                animController.PlayAnimationForwad();
            }
        }


    }

While animController.PlayAnimationForwad()description given in below script. It play the animation and set enum state:

public class AnimationController : MonoBehaviour
{

    [SerializeField]
    Animation anim;

    [SerializeField]
    string clipName;

    [Tooltip("The Animation Type that will be set after play")]
    public AnimationStates animationStateAfterPlay;

    private float animationSpeed = 0.5f;

    public static bool isAnyAnimationPlaying;

    private void Reset()
    {
        anim = GetComponent<Animation>();
        clipName = anim.name.ToString();
    }

    void Update()
    {
        isAnyAnimationPlaying = anim.isPlaying;//is any animation is playing. Then use this variable to block user inupt. like assets selection.
    }
    /// <summary>
    /// Play Animaton forwad
    /// </summary>
    public void PlayAnimationForwad()
    {

        anim[clipName].speed = animationSpeed;
        anim.Play();
        Debug.Log("PlayAnimationForwad and set state to" + animationStateAfterPlay);
        AnimationStateManager.SetCurrentState(animationStateAfterPlay);
    }
    /// <summary>
    /// Play animation backward/reverse
    /// </summary>
    public void PlayAnimationBackward()
    {
        anim[clipName].time = anim[clipName].length;
        anim[clipName].speed = animationSpeed * -1;
        anim.Play();
    }

    /// <summary>
    /// Wait until the animation has completed, for this reason. Multiply the animation Length with speed and get the Waiting Time.
    /// </summary>
    /// <returns></returns>
    public float GetCurrentAnimationCompleteTime()
    {

        return anim[clipName].length * ((1 * (1 / anim[clipName].speed) * -1));//the last -1 multiply is to get positive value. as speed is in negative format when reverse animation play.
    }

}
\$\endgroup\$
  • 1
    \$\begingroup\$ Generally it's advisable to code your game so that it doesn't depend on a specific ordering of components. As much as possible, we should think of component scripts as an un-ordered collection. Does this ordering change the behaviour of one of your scripts? If so, can you share with us the snippet of code that's impacted by the ordering? We may be able to help you find ways to make it order-independent, or enforce the logical ordering you need in a different way. \$\endgroup\$ – DMGregory Nov 27 at 15:06
  • \$\begingroup\$ Also, are any of the scripts static or singletons? Since instantiation occurs on the first, if a later component loads after an earlier one, static classes will already be instantiated regardless of execution order. \$\endgroup\$ – Jesse Williams Nov 27 at 17:14
  • \$\begingroup\$ @DMGregory i have update the question with the code. \$\endgroup\$ – Muhammad Faizan Khan Nov 28 at 6:13
  • \$\begingroup\$ @JesseWilliams the combination of script attached to different object in my scene so it is not singleton. \$\endgroup\$ – Muhammad Faizan Khan Nov 28 at 6:14
  • \$\begingroup\$ Thank you for removing the cross post. Can you clarify what the ordering problem is with the current code? The quicker you make it to understand your problem, the faster you'll tend to get good answers. If users have to read all your code and run it in their head to find the issue, it might take a lot longer. \$\endgroup\$ – DMGregory Nov 29 at 4:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.