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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Intended action only happens on final object in my foreach loop

Discussion in 'Scripting' started by marvelas1, Dec 2, 2019.

  1. marvelas1

    marvelas1

    Joined:
    Feb 16, 2016
    Posts:
    8
    Hello all,

    For an facetracking AR game I'm working on, I created a "manual" raycast if you will; Picture a long (invisible) collider sticking out from your nose that makes contact with 6 squares (buttons, for all intents and purposes) in a 2D UI menu. These 6 buttons sit underneath a larger rectangular slot; the intent being that the 6 buttons act as a "remote control" that changes whatever currently showing on the "TV" above. When the protruding beam makes prolonged contact with any square, its outline is enabled and a radial wipe begins on the large rectangular slot.

    All my collider stuff works fine.

    However, I noticed that the radial wipe only plays when I make contact with the last item in the squares array (in this case, Tile 6), and need help figuring out why.

    Here is [most of] my code. Lines 60-64 are my radial wipe. Editor screenshots are included below.

    Code (CSharp):
    1. [SerializeField]
    2.     private float requiredHoldTime;
    3.  
    4.     public UnityEvent onLongClick;
    5.  
    6.     //[SerializeField]
    7.     //public Image[] fillImage;
    8.     public Image[] fillImages;
    9.  
    10.  
    11.     void Start () {
    12.  
    13.     }
    14.    
    15.     void Update()
    16.     {
    17.         //RaycastHit theHit;
    18.  
    19.         if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out theHit))
    20.         {
    21.             for (int i = 0; i < 7; i++)
    22.                 if (pointerTarget.name == (string)theHit.collider.gameObject.name)
    23.                 {
    24.                     tiles2[i].GetComponent<Outline>().enabled = true;
    25.                 }
    26.                 else{
    27.                     tiles2[i].GetComponent<Outline>().enabled = false;
    28.  
    29.                 }
    30.         }
    31.  
    32.         // if outline is on, pointerDown is true
    33.         for (int i = 0; i < 7; i++)
    34.         {
    35.             if (tiles2[i].GetComponent<Outline>().enabled == true)
    36.             {
    37.                 pointerDown = true;
    38.                 Debug.Log("pointerDown = true");
    39.             }
    40.             else if (tiles2[i].GetComponent<Outline>().enabled == false)
    41.             {
    42.                 pointerDown = false;
    43.                 Debug.Log("pointerDown = false");
    44.             }
    45.  
    46.         }
    47.  
    48.  
    49.         if (pointerDown)
    50.         {
    51.             pointerDownTimer += Time.deltaTime;
    52.             if (pointerDownTimer >= requiredHoldTime)
    53.             {
    54.                 if (onLongClick != null)
    55.                     onLongClick.Invoke();
    56.  
    57.                 Reset();
    58.             }
    59.  
    60.             //radial wipe - seems to only affect the very last square in the array
    61.             foreach (Image image in fillImages)
    62.             {
    63.                 image.fillAmount = pointerDownTimer / requiredHoldTime;
    64.             }
    65.  
    66.         } else if (!pointerDown) {
    67.             Reset();
    68.         }
    69.            
    70.  
    71.     }
    72.  
    73.     private void Reset()
    74.     {
    75.         pointerDown = false;
    76.         pointerDownTimer = 0;
    77.  
    78.         foreach (Image image in fillImages)
    79.         {
    80.             image.fillAmount = pointerDownTimer / requiredHoldTime;
    81.         }
    82.     }
    Here is a screenshot of what happens when Button 6 is highlighted, which is intended:
    button6_hasRadialWipe.jpg
    Here, Buttons 1-5 do not activate the radial wipe:
    button2_missingRadialWipe.jpg

    Here is the Inspector:
    Screen Shot 2019-12-02 at 12.04.40 AM.png

    Thank you to anyone who has time to offer any insight on this!
     
  2. AlexN2805

    AlexN2805

    Joined:
    Nov 27, 2019
    Posts:
    33
    Code (CSharp):
    1.         for (int i = 0; i < 7; i++)
    2.         {
    3.             if (tiles2[i].GetComponent<Outline>().enabled == true)
    4.             {
    5.                 pointerDown = true;
    6.                 Debug.Log("pointerDown = true");
    7.             }
    8.             else if (tiles2[i].GetComponent<Outline>().enabled == false)
    9.             {
    10.                 pointerDown = false;
    11.                 Debug.Log("pointerDown = false");
    12.             }
    13.         }
    This is where the mistake is.
    Lets assume you hit tile 1 (or any other tile than the last one):
    First time it enters, tiles2[0].enabled = true... so it sets pointerDown to true and then continues on to the next tile. tiles2[1].enabled = false ... so it sets pointerDown to false now. Your debuglog will probably show something like true, false, false, false, false, false.
    If you change the above code to the code below, you will not overwrite pointerdown anymore after it's set to true:
    Code (CSharp):
    1.         for (int i = 0; i < 7 && !pointerDown; i++)
    2.         {
    3.             if (tiles2[i].GetComponent<Outline>().enabled == true)
    4.             {
    5.                 pointerDown = true;
    6.                 Debug.Log("pointerDown = true");
    7.             }
    8.             else if (tiles2[i].GetComponent<Outline>().enabled == false)
    9.             {
    10.                 pointerDown = false;
    11.                 Debug.Log("pointerDown = false");
    12.             }
    13.         }
     
    marvelas1 and GoliathAT like this.
  3. marvelas1

    marvelas1

    Joined:
    Feb 16, 2016
    Posts:
    8
    Thank you, AlexN2805!

    That worked like a charm :)