Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by OnyxRook · Jan 19, 2014 at 04:48 AM · javascriptinstantiatearrays

Instantiating prefabs to every object in an array?

Hey all,

I'm working on an office level, and I've opted to dynamically generate all of the decor for the cubicles. I've managed to get this working on a single cube, but now I'd like to expand it out to the rest using the same script. Here's what I've got so far:

 var computerLocation : Transform;
 var computerPrefabs : GameObject[];
 var plantLocation : Transform;
 var plantPrefabs : GameObject[];
 
 
 function Start() {
 
     GenerateDecor(); 
 }
 
 
 function GenerateDecor () {
 Instantiate (computerPrefabs[Random.Range(0, computerPrefabs.Length)],computerLocation.transform.position, computerLocation.transform.rotation);
 Instantiate (plantPrefabs[Random.Range(0, plantPrefabs.Length)],plantLocation.transform.position,Quaternion.identity);
 }

Every cube will have the same collection of object types, so it seems I should just make my location variables arrays... But that's where I become lost. I haven't worked with arrays much, so I'm not sure how to tell the script to add a random prefab to each location in the proposed array.

If someone could point me in the right direction, I'd greatly appreciate it!

Comment
Add comment · Show 4
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 getyour411 · Jan 19, 2014 at 04:41 AM 0
Share

Removed due to err

avatar image getyour411 · Jan 19, 2014 at 04:55 AM 0
Share

I think you are wanting to create a base class that defines what a 'cubicle' is, and also know how to use the default constructor to add things to a List of ; this might help

http://unity3d.com/learn/tutorials/modules/intermediate/scripting/inheritance

http://unity3d.com/learn/tutorials/modules/intermediate/scripting/lists-and-dictionaries

avatar image OnyxRook · Jan 19, 2014 at 06:28 AM 0
Share

Thanks for the reply! In regards to your first comment regarding Random.Range, I've seen all of the prefabs from my array appear during testing.. It's never been my experience that Random.Range wouldn't select the last object.

I'm not sure I understand what the use of creating a class would be in this situation. While I haven't used classes for anything before in Unity, the video you linked to explained it quite well. Could you explain why you think it would be worthwhile here?

As a side note I realized that I didn't mention in my first post that the locations are simply game empties. It's probably obvious and unimportant, but I figured I should mention it anyway!

avatar image getyour411 · Jan 19, 2014 at 07:04 AM 0
Share

Oops, sorry I did interpret that wrong while it's true that RandomRange(0,3) would never return 3 (int returns in inclusive on max) the zero-filled List would return 0,1,2 which is correct. You can verify the max-never-returned bit here

http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html

Re: the Class, it might be overboard to create a class Cubicle defining all the various properties; since you were creating an office it seemed like you might benefit from having a Cubicle class to add all the various things that might grow to be used for.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by OnyxRook · Jan 19, 2014 at 10:26 PM

Did a bit more research and discussed my problem with a couple of other devs, and the now very obvious answer made its appearance: a for loop.

Derp! I feel silly now. Documenting in case someone else has the same quandary at some point:

 function GenerateDecor(){
 for(var i : int = 0; i < computerLocation.length; i++)
 {
 Instantiate (computerPrefabs[Random.Range(0,computerPrefabs.Length)], computerLocation[i].position,computerLocation[i].rotation);
 }
 }

Changed my location variables to arrays as I had thought I should, and that's it. I'll just go study more javascript now...

Comment
Add comment · Show 2 · 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 getyour411 · Jan 19, 2014 at 10:28 PM 0
Share

Good deal. Oi, I thought you wanted to capture/add the Instantiated GameObjects into a List (i.e Cubicle)

avatar image OnyxRook · Jan 19, 2014 at 10:32 PM 0
Share

Haha, no, nothing so complex. I think half of my problem is that I kind of suck at explaining my scripting problems. Working on improving that! Thanks for trying to help out though!

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

18 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

Related Questions

Cannot add Instaniated gameObjects to a GameObject[] using Javascript. Error: "NullReferenceException: Object reference not set to an instance of an object" 2 Answers

How to spawn objects from CSV file 1 Answer

Accessing variable of instantiated prefab inside array in javascript 1 Answer

How do you spawn a random object from an array. 2 Answers

Instantiate prefab within parameters?(Javascript) 1 Answer


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