• 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 H03N · Apr 20, 2013 at 01:25 PM · prefabsarraysendless runner

How to create an endless track using an array of prefabs ?

Hi guys!

First of all, sorry for my bad english.

I'm creating an endless Runner. I followed this tutorial http://catlikecoding.com/unity/tutorials/runner/ Now I want to generate the track with a number of different level tiles, which randomly spawn in a row.

With the tutorial I did a Manager that uses only one prefab. So I think I need an array of prefabs which are randomly initiating to the track. But I don't know how to do it

For the Array I wrote:

 var levelTiles : GameObject[];
 var levelTile1: GameObject;    
 var levelTile2: GameObject;
 var levelTile3: GameObject;
 
 levelTiles[0] = levelTile1;    
 levelTiles[1] = levelTile1;
 levelTiles[2] = levelTile2;

BUT I don't what I ave to do now.

Can something please help me ? I'm totally new at this.

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 whebert · Apr 20, 2013 at 01:55 PM

You can simply use:

 public var levelTiles : GameObject[];

You will then be able to populate that array in the Inspector by setting the size of your array and drag prefabs into each element.

alt text

Then choose randomly like so:

 //Choose new tile, assuming there is at least 1 item in array
 var randomTileIndex : int = Random.Range(0, levelTiles.Length);
 var myRandomTile : GameObject = levelTiles[randomTileIndex];



leveltilesmanager.jpg (47.5 kB)
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
avatar image
0

Answer by H03N · Apr 21, 2013 at 03:03 PM

It works perfect for me :D And it's so simple Thanks !!

Comment
Add comment · Show 1 · 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 whebert · Apr 21, 2013 at 10:09 PM 0
Share

If you believe the question answered, mark the answer as correct.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

How Do I Create Random Platforms in a 2d Game? 0 Answers

generating unique names for prefabs within an array 2 Answers

hi!does any know how to make an endless track 1 Answer

Drag and Drop onto Array of Custom Objects 0 Answers

Trouble instantiating an Array of Prefabs 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