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 diddydale_95 · Feb 12, 2014 at 12:32 PM · prefabrandomrunnergenerator

Creating a random prefab generator in C# for endless runner

Hey Everyone, I'm making an endless runner game for my current college (UK) project, but I'm unable to create a script that generates a random prefab (from a list of prefabs) one after the other. Any help with this would be appreciated so much as I am relatively new to both Unity and C#, Thanks! Dale

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

3 Replies

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

Answer by iamvishnusankar · Feb 12, 2014 at 12:49 PM

I don't recommend you to Call this CreatePrefab() function inside Update() function. As it will create infinite number of prefabs and will decrease your game performance, So I recommend you to have Entry or exit trigger to instantiate the prefab objects. :)

Also check this question : [Infinite Runner World creation [sloved]][1]

 public GameObject[] _myPrefabs;
 
     void Start()
     {
 
         CreatePrefab();
     }
     
     void CreatePrefab()
     {
         GameObject clone = Instantiate(_myPrefabs[RandomNumber()])as GameObject;
     }
 
     int RandomNumber()
     {
         System.Random rand = new System.Random();
         return rand.Next(0,_myPrefabs.Length);
     }
 


[1]: http://answers.unity3d.com/questions/637266/infinite-runner-world-creation.html

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 diddydale_95 · Feb 12, 2014 at 02:43 PM 0
Share

Thanks for responding so quickly :) after looking at both of the answers you provided, I decided to use the box collider method. I was just wondering what I would have to write so that once a prefab contacts the box collider it will be destroyed and instantiate a new random prefab? Once again thank you so much for taking time to help me.

avatar image iamvishnusankar · Feb 12, 2014 at 03:12 PM 0
Share

don't forget to leave an acceptance for this answer if you found its helpful. Cheers :-)

avatar image
1

Answer by Crax · Feb 12, 2014 at 04:10 PM

You may use the Random.Range function and a switch/case cicle. EDIT: Sorry, i forgot to write that this is in Javascript. It may be useful for who is searching the answer but doesn't write code in C#

Example:

 #pragma strict
 private var rand:int;
 
 function FixedUpdate(){
 rand=Random.Range(1,4);
 case 1:
 Instantiate(<prefab1>);
 break;
 case 2:
 Instantiate(<prefab2>);
 break;
 case 3:
 Instantiate(<prefab3>);
 break;
 case 4:
 Instantiate(<prefab4);
 break;
 }

Maybe it is not the best way... P.S Remember to change your physics timestep.

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 Hamilcar-games · Jun 10, 2017 at 09:51 AM

why case you just create

int rnd ;

rnd=Random.Range(0,5);

instantiate(tile[rnd],transform.position,transform.rotation); ... . .

. .. . .

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

21 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

Related Questions

Randomly generated number 0 Answers

How can I make my prefab fire out at a random velocity every 2 secs 2 Answers

Random.Range(..) not working 1 Answer

instantiate random prefabs based on player camera distance 1 Answer

Weighted Item Generator returns same Item 2 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