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 /
avatar image
1
Question by Kitty · Mar 09, 2010 at 02:43 PM · gameobjectinstantiate

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.

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

2 Replies

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

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.

Comment
Add comment · Show 8 · 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
avatar image Kitty · Mar 10, 2010 at 04:34 PM 0
Share

How would I get it to show up in the center of the random boxes with the script attached to an empty GameObject?

avatar image duck ♦♦ · Mar 10, 2010 at 04:48 PM 0
Share

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?

avatar image Kitty · Mar 10, 2010 at 07:49 PM 0
Share

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.

avatar image duck ♦♦ · Mar 10, 2010 at 08:56 PM 0
Share

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?

avatar image Kitty · Mar 10, 2010 at 10:15 PM 0
Share

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.

Show more comments
avatar image
2

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.

Comment
Add comment · Show 5 · 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
avatar image Kitty · Mar 15, 2010 at 02:57 PM 0
Share

It's suppose to read "i

avatar image duck ♦♦ · Mar 15, 2010 at 03:45 PM 0
Share

(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!)

avatar image duck ♦♦ · Mar 15, 2010 at 03:53 PM 0
Share

so - this code generates the desired result?

avatar image Kitty · Mar 15, 2010 at 08:45 PM 0
Share

Ah! Thanks for the edit. And about. It doesn't fill in all 118 cubes, just up to 38. Any ideas why?

avatar image Kitty · Mar 16, 2010 at 02:46 PM 0
Share

Haha I got it! I went back and found out that I had labelled something by the same number twice. XD

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

No one has followed this question yet.

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

Unable to call another gameObject's script 4 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