Giving Instantiated Clones their own properties and variables
Hi,
My question is regarding an issue that i'm having when trying to code in a function which allows me to instantiate clones of a sphere (star) and give each clone the following properties:
Star Class (O, B, A, F, G, K, M)
Star Temperature (which is determined by the star class)
Star ID (Randomly generated number but is purely for aesthetic purposes)
Star Colour (the colour is taken from research data and is basically an array of RBG values, also dependent on the starclass)
Sorry if the code I paste isn't in special formatting. New to forum posting.
START
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SpawnItems : MonoBehaviour {
public Transform[] SpawnPoints;
public float SpawnTime = 0.1f;
public GameObject stars;
public bool starscloned;
public GameObject newObject;
public float starID;
public string starclass;
private string starcolor;
public int startemp;
public float randomint;
public GameObject star;
public int generatedclass;
// Use this for initialization
void Start ()
{
Invoke ("SpawnStars", SpawnTime);
// When this script starts, the function "SpawnStars()" is forced to run; hence, cloning 20 stars
}
//*************************************************************************************************************************************
public void SpawnStars()
{
bool starscloned = false;
// Flag for while loop
Debug.Log(SpawnPoints.Length);
int spawnIndex = 0;
/*
* The spawnindex stars at 0 then after every instatiation; 1 is added to it.
* Right before the loop iterates, an IF test checks to see if the spawnindex is equal to the max size of the array
* If so, the flag clonedbool breaks the while loop
*/
Vector3 randomPosition;
while (starscloned == false)
{
randomPosition = new Vector3(Random.Range(-500f, 500f), Random.Range(0f, 700f), Random.Range(-500f, 500f));
//The Vector called randomPosition is randomly assigned X, Y and Z co-ordinates by the "Random.Range" function.
// The generated Y co-ordinate must be between 0 and some number because a star can never be below the horizon.
//https://forum.unity3d.com/threads/randomly-generate-objects-inside-of-a-box.95088/
randomPosition = transform.TransformPoint(randomPosition * 2.5f);
// The randomPosition is maximised and scaled appropriate to the scene.
newObject = Instantiate (stars, randomPosition, SpawnPoints [spawnIndex].rotation);
/*
* Instantiate generates a clone of an object. The function is divided into 3 components;
* Object, Position, Roation
* The object is the model which is being cloned
* The Position is the position where the object will be spawned
* Quaternion is the rotation component of the new spawn
*/
newObject.name = ("StarClone[" + spawnIndex + "]");
// The new Object is intrinsictly named for future use
spawnIndex = spawnIndex + 1;
//Essentially a counter in itself
if (spawnIndex == SpawnPoints.Length)
{
starscloned = true;
}
// Breaks the while loop when the stars have been cloned. SpawnIndex is compared to SpawnPoints.Length.
int timescalled = 0;
timescalled = timescalled + 1;
Debug.Log ("Times called = " + timescalled);
Invoke ("Classify", 0f);
}
Debug.Log ("It escaped the loop");
}
//*************************************************************************************************************************************
public void Classify()
{
Debug.Log ("The classify function has been reached");
// The starID is simply a float which has its value determined by a random number in the range of 1001 to 9999.
// This is purely for design purposes.
starID = Random.Range (1001, 9999);
// Initialising the star class.
starclass = "O";
// Generating a random integer between 0 and 6 which represent the 7 possible star types (soon to be assigned).
randomint = Random.Range (0, 6);
randomint = Mathf.Round (randomint);
Debug.Log ("Randominteger after it has been rounded is " + randomint);
// Casewhere randomint is...
switch ((int)(randomint*100))
{
case 0:
starclass = "O";
// The startemp is in the range between the commonly found temperatures. Temperatures seen on the page below.
// https://en.wikipedia.org/wiki/Stellar_classification
startemp = Random.Range (30000, 50000);
break;
case 1:
starclass = "B";
startemp = Random.Range (10000, 29999);
break;
case 2:
starclass = "A";
startemp = Random.Range (7500, 9999);
break;
case 3:
starclass = "F";
startemp = Random.Range (6000, 7499);
break;
case 4:
starclass = "G";
startemp = Random.Range (5200, 5999);
break;
case 5:
starclass = "K";
startemp = Random.Range (3700, 5199);
break;
case 6:
starclass = "M";
startemp = Random.Range (2400, 3699);
break;
}
generatedclass = generatedclass + 1;
Debug.Log ("I have generated the star class " + generatedclass + " times.");
Debug.Log("The starclass is now " + starclass);
Debug.Log ("The temperature of the star is now " + startemp);
// In the case where starclass is O|B|A|F|G|K|M , change the starcolor according to the class
// http://www.vendian.org/mncharity/dir3/starcolor/
switch (starclass)
{
case "O":
// This line retrieves the color of the material on GameObject and assigns a new color onto it.
// The RGB colors were sourced from the link above in the comments.
Debug.Log ("Should change color to O");
star.GetComponent<Renderer>().material.color = new Color(155, 176, 255);
break;
case "B":
Debug.Log ("Should change color to B");
star.GetComponent<Renderer>().material.color = new Color(170, 191, 255);
break;
case "A":
Debug.Log ("Should change color to A");
star.GetComponent<Renderer>().material.color = new Color(202, 215, 255);
break;
case "F":
Debug.Log ("Should change color to F");
star.GetComponent<Renderer>().material.color = new Color(248, 247, 255);
break;
case "G":
Debug.Log ("Should change color to G");
star.GetComponent<Renderer>().material.color = new Color(255, 244, 234);
break;
case "K":
Debug.Log ("Should change color to K");
star.GetComponent<Renderer>().material.color = new Color(255, 210, 161);
break;
case "M":
Debug.Log ("Should change color to M");
star.GetComponent<Renderer> ().material.color = new Color (255, 204, 111);
star.GetComponent<Material> ().color = new Color (255, 204, 211);
break;
}
Debug.Log ("The script made it through the part where colors are set");
}
}
END.
Essentially I want each clone to have their own class. This class is randomly generated by an integer which is rounded and using in a switch case. I'm pretty sure that the variables have to be directly assigned to each clone using their [spawnindex] but I am having trouble coding it in.
Any comments or ideas are greatly appreciated.
Valkarth, signing out!
Answer by Valkarth · Apr 19, 2017 at 06:01 AM
FIXED:
randomintfloat = Random.Range (0, 6);
int randomint = (int)Mathf.Round (randomintfloat);
Debug.Log ("The value of randonint after it has been rounded is " + randomint);
// Casewhere randomint is...
switch (randomint)
You have to force convert the float to an integer. Also, you must place the script on the prefab so each clone carries the script.