I have three components
of script
which are attached to different objects
DisplayMenu
PlayAnimationOnMouseDown
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
PlayAnimationOnMouseDown
DisplayMachinePartName
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.
}
}