How can I update (reset) the number of objects

I am trying to build this game: a user defines an integer called “samplesize”, and a number called “ylocation”, and exactly “samplesize” spheres appear on the scene, equally spaced along the x-axis, and shifted upwards to “ylocation”.

If the user enters new values for samplesize" and “ylocation”, then the scene changes to reflect the new number of spheres and locations.

Here are my problems so far:
(i) When I set a samplesize, “supernumerary spheres” left over from the previous scene do not disappear. How do I make them disappear? For example, if a scene has 1000 spheres, and I redefine samplesize to 5, and click OK, then I want to see just 5 spheres (not 1000).

(ii) How do I make the game start with no spheres. I do not want any spheres to appear on the scene until I have set a samplesize and clicked “OK”. But Unity (C#?) starts by placing all 1000 spheres at location (0,0,0). How do I stop Unity from doing this?

Here is my code to set up the GUI for data entry

using UnityEngine;
using System.Collections;

public class ButtonText : MonoBehaviour
{	
private bool defineModel = false;
string beta0 = "" ,  beta1 = "";
public float  ylocation ;
public int samplesize=0 ;

void OnGUI()
{
	if (!defineModel) {
		if (GUI.Button (new Rect (0, 0, 150, 20), "Define a model"))
			defineModel = true;
	} else {  
		beta0 = GUI.TextField (new Rect(10, 10, 50, 25), beta0, 40);
		beta1 = GUI.TextField (new Rect(10, 40, 50, 25), beta1, 40);

		if (GUI.Button (new Rect (300, 250, 100, 30), "OK")) {
				samplesize = int.Parse(beta0);
				ylocation = float.Parse(beta1);
				defineModel = false;
				return; 
		}
	}
}

}

Here is my code to draw the scene:

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;

public class SphereManager : MonoBehaviour
{
public List<GameObject> spheres; // declare a list of spheres
void Awake () {
	
	for (int i = 0; i < 1000; i++) {
		GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); // Make a new sphere
		Rigidbody gameObjectsRigidBody = sphere.AddComponent<Rigidbody>(); // Add the rigidbody.
		gameObjectsRigidBody.useGravity = false; // turn off the sphere's gravity.
		sphere.collider.enabled = false;         // remove the collider
		spheres.Add(sphere);                    // add the sphere to the end of the list
		
		
	}
}

void Update()  
{ 		
	int n = GetComponent<ButtonText> ().samplesize;
	float ylocation = GetComponent<ButtonText> ().ylocation;

	for(int i=0; i<n; i++) {
		spheres*.transform.position = new Vector3(i, ylocation, 0); // reposition the sphere*
  •   } 		*
    
  • }*
    }
    Any advice would be appreciated. Thanks.

In response to your comment, Update() is called every frame, Awake() and Start() are called at initialization. You have to write your own method to spawn spheres that you call when you press a button, and that same button press should also destroy all your previously made spheres.

So first loop through your sphere’s list and remove and destroy every sphere in it, then add new spheres in a loop (and don’t forget to add them to your list).

Thank you ; that was very helpful. Following your advice, I emptied Awake() as well as Update(), moving everything over to OnGui().
At the click of OK the GUI script now destroys all spheres, and spawns new spheres ; the software now behaves the way I want.

My GUI script “ButtonText” now looks like:

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;

public class ButtonText : MonoBehaviour
{	
private bool defineModel = false;
string beta0 = "" ,  beta1 = "";
public float  ylocation ;
public int samplesize=0 ;
public List<GameObject> spheres; // declare a list of spheres

void OnGUI()
{
	if (!defineModel) {

		if (GUI.Button (new Rect (0, 0, 150, 20), "Define a model"))
			defineModel = true;

	} else {  
		beta0 = GUI.TextField (new Rect(10, 10, 50, 25), beta0, 40);
		beta1 = GUI.TextField (new Rect(10, 40, 50, 25), beta1, 40);

		if (GUI.Button (new Rect (300, 250, 100, 30), "OK")) {

			for (int i = 0; i < spheres.Count; i++) {  Destroy( spheres *); } // empty the list* 
  •   		samplesize = int.Parse(beta0);*
    
  •   		ylocation = float.Parse(beta1);*
    
  •   		if (samplesize > 0) {*
    
  •   		for (int i = 0; i < samplesize; i++) {*
    
  •   			GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); // Make a new sphere*
    
  •   			Rigidbody gameObjectsRigidBody = sphere.AddComponent<Rigidbody>(); // Add the rigidbody.*
    
  •   			gameObjectsRigidBody.useGravity = false; // turn off the sphere's gravity.*
    
  •   			sphere.collider.enabled = false;         // remove the collider*
    
  •   			sphere.transform.position = new Vector3(i, ylocation, 0); // reposition the sphere*
    
  •   			spheres.Add(sphere);*
    
  •   			}}*
    
  •   			defineModel = false;*
    
  •   			return;* 
    
  •   	}*
    
  •   }*
    
  • }*
    }