Home>

I want to judge by the If statement that the Inspector of the object created by the Button of Canvas has Position Y = 102, and if it is True, execute the method.

The output of Debug.Log (Middle.transform.localPosition.y) is 102, but If (Middle.transform.localPosition.y == 102) doesn't seem to be True, it doesn't work. I would appreciate it if you could tell me how to solve it.

Corresponding source code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using TMPro;
public class UIManager: MonoBehaviour
{
    private TextMeshProUGUI levelText;
    int index;
    public GameObject Top;
    public GameObject Middle;
    public GameObject Bottom;
    public GameObject ConfettiTop;
    public GameObject ConfettiBottom;
    GameObject TopHigh;
    GameObject TopLow;
    GameObject Middle High;
    GameObject MiddleLow;
    GameObject BottomHigh;
    GameObject BottomLow;
    // public delegate void SetCurrentState (GameState state);// Type
    // public SetCurrentState setCurrentState;// Function types that can be registered externally
    // Start is called before the first frame update
    void Start ()
    {
        index = SceneManager.GetActiveScene (). buildIndex;
        index ++;
        changeLevel ();
    }
    // Update is called once per frame
    void Update ()
    {
    }
    public void changeLevel ()
    {
        // Initialize each scene using index (I want to use it in the Level notation of UiManager)
        Debug.Log ("changeLevel ()" + index);
        levelText = this.GetComponent<TextMeshProUGUI>();
        Debug.Log (levelText);
        levelText.text = "Level" + index;
    }
    public void Clear ()
    {
        if (index == 1)
        {
            Debug.Log (Middle.transform.localPosition.y);
            //Top.transform.localPosition.y == 0f ||
            if (Middle.transform.localPosition.y == 102)
            {
                StartCoroutine (Confetti ());
                Debug.Log ("Clear !!");
            }
        }
        else if (index == 2)
        {
            MiddleHigh = GameObject.Find ("MiddleHigh");
            MiddleLow = GameObject.Find ("MiddleLow");
            if (MiddleHigh.transform.localPosition.y == 74.9f&&Top.transform.localPosition.y == 0&&MiddleLow.transform.localPosition.y == -145&&Bottom.transform.localPosition.y == -74.9f)
            {
                StartCoroutine (Confetti ());
            }
        }
        else if (index == 3)
        {
            TopHigh = GameObject.Find ("TopHigh");
            TopLow = GameObject.Find ("TopLow");
            MiddleHigh = GameObject.Find ("MiddleHigh");
            MiddleLow = GameObject.Find ("MiddleLow");
            BottomHigh = GameObject.Find ("BottomHigh");
            BottomLow = GameObject.Find ("BottomLow");
            if (TopHigh.transform.localPosition.y == 44&&TopLow.transform.localPosition.y == 132&&MiddleHigh.transform.localPosition.y == -88&&MiddleLow.transform.localPosition.y == 0&&BottomHigh.transform .localPosition.y == 44)
            {
                StartCoroutine (Confetti ());
                Debug.Log ("Clear !!");
            }
        }
    }
     IEnumerator Confetti ()
    {
        ConfettiTop.SetActive (true);
        yield return new WaitForSeconds (1);
        ConfettiBottom.SetActive (true);
    }
}
What I tried

I also tried it when Middle.transform.localPosition.y was 102 ~ 103, but it didn't become True.

if (index == 1)
        {
            Debug.Log (Middle.transform.localPosition.y);
            //Top.transform.localPosition.y == 0f ||
            if (102<= Middle.transform.localPosition.y&&Middle.transform.localPosition.y<= 103)
            {
                StartCoroutine (Confetti ());
                Debug.Log ("Clear !!");
            }
        }
  • Answer # 1

    Since the y property is a float type, it contains a decimal number. Is it slightly off from 102?
    If this is the cause, you can solve it by converting the value obtained from the y property to an int type.

Related articles