title: Unity - How to add GameObjects to an array and attach RigidBody2D so they move up and down
body: <p>I would like to add few objects (prefabs) (tagged: MovingCloud) (quantity is different from scene to scene). Script is attached to this prefab.
  I would like all these clouds being detected and move up and down. I tried with arrays but don't know how to make it work.</p>
  
  <pre class="lang-cs prettyprint-override"><code>GameObject[] clouds;
  GameObject cloud;
  Rigidbody2D rb;
  
  float cloudSpeed = 2f;
  void Start () {
      clouds = GameObject.FindGameObjectsWithTag ("MovingCloud");
  
      foreach (GameObject cloud in clouds) {
          var rb = cloud.GetComponent  &lt;Rigidbody2D&gt; ();
      }
  }
  
  void FixedUpdate () {
      if (transform.position.y &lt; - 3f) {
          rb.velocity = new Vector2 (0, cloudSpeed);
      }
  }
  </code></pre>