Randomly spawn the same object with different properties consistently.

I’m trying to create a randomly spawning object similar to a hot air balloon. I want the hot air balloon to rise and fall, to Y mins and Y maxes, my issue is, I’m not sure what route I should take to make each consecutive balloon spawmn with a different Y min and Y max, here is the code I’m using to simulate this:

using UnityEngine;
using System.Collections;

public class BalloonScript : MonoBehaviour {

	// Variables for floating functions
	public Vector2 speed = new Vector2(0,0);
	public float bobSpeed = 3.0f;  //Bob speed
	public float bobHeight = 0.5f; //Bob height
	public float bobOffset = 0.0f;
	
	public float PrimaryRot = 80.0f;  //First axies degrees per second
	public float SecondaryRot = 40.0f; //Second axies degrees per second
	public float TertiaryRot = 20.0f;  //Third axies degrees per second
	private float pos;
	private float bottom;
	//^^^Variables for floating functions^^^//
	
	void Awake () {

		bottom = transform.position.y;
	}	

	void Update () {
		

		Vector2 movement = new Vector2 (speed.x, speed.y );
		movement *= Time.deltaTime;
		transform.Translate (movement);

		//transform.Rotate(Vector3(0, PrimaryRot, 0) * Time.deltaTime, Space.World);
		//transform.Rotate(Vector3(SecondaryRot, 0, 0) * Time.deltaTime, Space.Self);
		//transform.Rotate(Vector3(0, 0, TertiaryRot) * Time.deltaTime, Space.Self);

		pos = bottom + (((Mathf.Cos((Time.time + bobOffset) * bobSpeed) + 1) / 2 ) * bobHeight);
		Vector2 newPosY = new Vector2(transform.position.x, pos);
		transform.position = newPosY;
		
		
	}
}

I tried to use Random.Range in this script, however, leaving this in the update function makes each balloon act erratically as the Y value (bobHeight) is flying all over the place. If I leave it in any non-update function, the balloons will all spawn each second, with the same bob height. Here is the spawn script:

using UnityEngine;
using System.Collections;

	public class NewScript : MonoBehaviour {
	public GameObject enemyPrefab;
	private GameObject newParent;
	public float xMin = 19F;
	public float xMax = 85F;
	public float yMin = 3.5F;
	public float yMax = -4.5F;

	// Use this for initialization
	void Start () {

		newParent = GameObject.Find("2-Middleground");

		InvokeRepeating ("SpawnUpdate", 1f, 1f);
		
	}
	void SpawnUpdate() {
		Vector3 newPos = new Vector3(Random.Range(xMin, xMax), Random.Range(yMin, yMax), 0);
		GameObject octo = Instantiate(enemyPrefab, newPos, Quaternion.identity) as GameObject;
		octo.transform.parent = newParent.transform;
		}
}

So, my question put more clearly is. I want these balloons to spawn once per second, each consecutive balloon has a different bobHeight and bobSpeed within the same range of numbers which are randomly generated (perhaps in to an array). The balloon has the first script attached to it, and the balloon has a prefab which is what the 2nd script spawns every second.

The other thing I tried was just making an animation curve, but that locks my X and Z positions which I don’t want.

What route should I take here?

You have two options:

  1. (Prefered) - right after instantiating a balloon, set its yMax and yMin properties randomly.
  2. In the Start() - set the yMin and yMax randomly (make sure max > min)

Any initialization should be done in Start() or Awake(). In Update, keep to the continous behaviour. In your case, the continous behaviour is moving between min and max, and there is no random there. You do want to randomize min and max, but you only need to do it once per object, so do it right after instantiating, or from the start.

Solution pseudo code

class balloon {

    public Vector2 speed = new Vector2(0,0);
    public float bobSpeed;  
    public float bobHeight; 
    public float bobOffset;
    ....

    void Awake (){
        bobSpeed = Random.Range (0f,5f);
        bobHieght = Random.Range (0f,5f);
        bobOffset = Random.Range (0f,5f);
        ....
    }

    void Update (){
        // Existing movement code
    }
}