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.