Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Valkarth · Apr 19, 2017 at 05:28 AM · c#gameobjectrenderingcolorarrays

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

346 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Vertex colors not working in shader graph. 0 Answers

My code has some invalid arguments 1 Answer

My gameobject disappears when I transform it 1 Answer

collider.gameObject.GetComponent doesn't have correct values yet it has the correct name 0 Answers

Status Effect Help 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges