What's the best design to use?

Hello,

I have experience with game development but can’t get my head around the gameobject/component/script model in Unity.

I want to create a simple game with bubbles. There will be say 20 bubbles on the screen. Each bubble should have a different position and a speed.

What’s the best way to do this?

Should I create a bubble prefab and create 20 instances of it? And if I want to control the movement from one script, where will I put it? Or will there be just one game-object, containing the bubbles somehow?

Thank you all for your help - I really appreciate it.

Best would be to fill a pool of bubble objects in the beginning using a prefab with a bubble script attached to it. So then each individual bubble has its own script, if needed.

You can store the list (or pool) or bubbles in a script, which you attach to the camera or any other object you wish. This script then controls the bubbles as you wish, setting speed and position and what nots:

// Main script that controls the bubbles...

Public GameObject orginalBubble = null;
Awake()
{
 // Create your bubbles with the orginalBubble object and store in a list

    createdBubble.SetPosition(new Vector3(1.0f, 0.0f, 0.0f);
 
}

Update()
{
 // Go through all bubbles and update them as you go on

Bubble*.SetSpeed(1.0f);*

}
And then the script that is attached to each individual bubble:
// Bubble script

private float speed = 0.0f;
Awake()
{
// Do some initialization here…
speed = 0.0f;
}
public void SetPosition(Vector3 newPosition)
{
this.Transform.Position = NewPosition;
}
public void SetSpeed(float newSpeed)
{
speed = newSpeed;
}
Update()
{
// Translate position with speed and so on…
}

I hope this example help you a little!
Good luck!

There are several possibilities. The most traditional would use an empty game object containing the control script, which would instantiate the bubbles based on a prefab, save their references in an array and control their movement - something like this:

var bubble: Transform; // drag the prefab here
var quant: int = 20; // how many bubbles to create
var bubbles: Transform[]; // save the bubble references here
var speeds: Vector3[]; // save the bubble velocities here

function Start(){
  bubbles = new Transform[quant]; // allocate arrays
  speeds = new Vector3[quant];
  for (var i=0; i<quant; i++){
    var pos: Vector3 = ... // create a random position...
    var rot: Quaternion = Random.rotation; // create a random rotation
    bubbles *= Instantiate(bubble, pos, rot); // create the bubble*

speeds = Random.onUnitSphere * Random.Range(minSpeed, maxSpeed); // assign a random velocity
}
}

function Update(){ // move the bubbles
for (var i=0; i<quant; i++){
bubbles.Translate(speeds * Time.deltaTime);
}
}
Other possibilities could be to have the bubbles moving themselves (require the movement script assigned to each bubble), or manually place the bubbles and define their velocities in the Editor(also require a script in each bubble)