- Home /
Instantiate a GameObject multiple times
I have imported my Uuo element from 3ds Max. Now that I have done that I need to figure out the best way to go about to taking that GameObject and making it appear in multiple boxes with the color number of electron, protons, neutrons, and shells depending on whatever element is on the box.
I remember reading a little about instantiating a GO, but not sure if that would be the best way to approach this. If so, what else do I need to look for and figure out? If not, suggestions?
Any help/guidance would be wonderful for this little educational project of mine..... Thanks.
Answer by duck · Mar 09, 2010 at 05:20 PM
Yes, instantiating sounds ideally suited to what you want to do.
Do you need to instantiate various numbers of electrons, protons and neutrons in the correct configuration?
Typically, you'd create a public variable in your script in which to store a reference to the prefab you want to instantiate. This variable is often of type Transform or GameObject (although in some instances it's more suitable to make the variable match the type of a script placed on your prefab). I'm using GameObject in this example:
var electronPrefab : GameObject;
You would then need to make sure that your script has a reference to the electron prefab dragged into this variable. You could then use Instantiate in that script to create instances of the prefab.
You can change the colour of an instantiated object by retaining the reference passed back by Instantiate, and then modifying the renderer material colour, like this:
GameObject obj = Instantiate( electronPrefab, position, rotation);
obj.renderer.material.color = Color.red;
Of course you can also specify custom colors, as well as using built-in colour names, like this:
obj.renderer.material.color = Color(0.8, 0.2, 0.8, 1);
Hope this is enough to get you started.
How would I get it to show up in the center of the random boxes with the script attached to an empty GameObject?
You can specify the position that the new instance should be, in the 2nd parameter of the Instantiate function. Do you want to select a random box first, and then instantiate the GO in that box's position?
In a way, yes. You've seen a periodic table, so it wouldn't really be random. The element itself is going to end up changing the number of protrons, neutrons, and electrons depending on which box it is. I'd like for the instantiate to be in the center of each box, kinda like how images are etched into crystal boxes.
So, are you looking to instantiate each separate proton, neutron and electron in the correct configuration inside the box? What do you have so far as a prefab? Do you have the information about how many of each type of particle and its position in some format which you can use as a look-up table?
Yes. The prefab is set up as a parent (Element) which has 4 children (Electrons, Protons, Neutrons, Nucleus, Shells). The children, except for Nucleus, have children of their own:e0-e117, p0-p27, n0-n27, and Shell01-Shell07. So, yes, I do have a look-up table.
Answer by Kitty · Mar 15, 2010 at 02:51 PM
I know this could be added to the comment chain above, but thought it would be easier to look over if it was below in it's own. This code is attached to an empty game object that I'm using to help me get my element into multiple cubes.
var prefab : GameObject;
function Start () { for (i=0; i < GameObject.Find("Table5").transform.childCount; i++) { Instantiate(prefab, GameObject.Find("Table5").transform.Find("" + (i+1)).transform.position, Quaternion.identity); } }
Now for the next step: Getting the correct number of the structure to show in the correct box.
(code formatting wasn't working because you were using html style formatting tags, rather than using the "code format" button - which simply indents all code by 4 spaces!)
Ah! Thanks for the edit. And about. It doesn't fill in all 118 cubes, just up to 38. Any ideas why?
Haha I got it! I went back and found out that I had labelled something by the same number twice. XD
Your answer
![](https://koobas.hobune.stream/wayback/20220612101831im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Clone Selector 1 Answer
Change a Script Var of an Instantiated Object 1 Answer
Detect when instance has deleted 0 Answers
How to set/get/change variables for newly spawned gameObjects? 1 Answer