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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Foreach loop not running multiple times

Discussion in 'Scripting' started by Lexo18, Apr 16, 2015.

  1. Lexo18

    Lexo18

    Joined:
    May 12, 2013
    Posts:
    34
    The foreach loop only runs once and does not update any of the variables inside past the first run through
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Closestlock : MonoBehaviour {
    5.     //Holds all GameObjects in the scene with the tag of Targetable.
    6.     GameObject [] targetable;
    7.     //The closest GameObject
    8.     public GameObject targeted;
    9.     // Use this for initialization
    10.     // Used to calculate which GameObject is closer.
    11.     public float minDist;
    12.     public float dist = 0;
    13.     // how quickly you want to be able to look at the target.
    14.     public float speed;
    15.  
    16.     void Start () {
    17.         //Fills the array with GameObjects
    18.         //uncomment this line if you do not need to check if new objects are being instantiated
    19.         //targetable = GameObject.FindWithTag("Targetable");
    20.     }
    21.    
    22.     // Update is called once per frame
    23.     void Update () {
    24.         //The on/off switch
    25.         bool targeting = true;
    26.         //Fills the array with GameObjects
    27.         targetable = GameObject.FindGameObjectsWithTag ("Targetable");
    28.  
    29.         //Runs through each GameObject
    30.         StartCoroutine (Locking ());
    31.         if (targeting) {
    32.             // lock on target
    33.             Quaternion targetRotation = Quaternion.LookRotation (targeted.transform.position - transform.position);  
    34.             // Smoothly rotate towards the target point.
    35.             transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, speed * Time.deltaTime);
    36.    
    37.         }
    38.     }
    39.  
    40.     //Locking
    41.     IEnumerator Locking(){
    42.             foreach (GameObject target in targetable) {
    43.                 //Stores the shortest distance and sets "targeted" to the shortest distance.
    44.                 dist = Vector3.Distance (transform.position, target.transform.position);
    45.            
    46.                 //checks to see which is closer
    47.                 if (dist < minDist) {
    48.                     minDist = dist;
    49.                     targeted = target;
    50.                
    51.                 } else if (minDist == 0) {
    52.                     targeted = target;
    53.                     minDist = dist;
    54.                 }
    55.            
    56.             }
    57.         yield return null;
    58.     }
    59. }
     
  2. Pavlon

    Pavlon

    Joined:
    Apr 15, 2015
    Posts:
    191
    Check if line 27 Return Null or 0
     
  3. Lexo18

    Lexo18

    Joined:
    May 12, 2013
    Posts:
    34
    When the script first runs it looks at the first object in the scene but it won't change what it looks at if another object comes close to it and the only variables that are not updating properly is dist and minDist