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 /
  • Help Room /
avatar image
0
Question by ElaynYT · Apr 07, 2018 at 05:26 AM · gameobjectgame

How Do I Randomize Infinite Platform (With Respect To This Code)?

I Have Made A C# Code Which Spawns Infinite Platform . I Have Also Made A Public GameObject Array With Which I Have Put In My Prefabs. Here's My Code:-

 #region Variables
 public GameObject[] TilePrefabs;
 private Transform playerTransform;
 private float spawnZ = 0.0f;
 private float tileLength = 31.0f;
 private int amnTilesOnScreen = 6;
 private float safeZone = 18.0f;

 private List<GameObject> activeTiles;
 #endregion

 #region Unity Methods
 private void Start()
 {
     activeTiles = new List<GameObject>();
     playerTransform = GameObject.FindGameObjectWithTag("Player").transform;


     for (int i = 0; i < amnTilesOnScreen; i++)
     {
         SpawnTile();
     }
 }

 private void Update()
 {
     if (playerTransform.position.z - safeZone > (spawnZ - amnTilesOnScreen * tileLength))
     {
         SpawnTile();
         DeleteTile();
     }
 }

 private void SpawnTile(int prefabIndex = -1)
 {
     GameObject go;
     go = Instantiate(TilePrefabs[0]) as GameObject;
     go.transform.SetParent(transform);
     go.transform.position = Vector3.forward * spawnZ;
     spawnZ += tileLength;
     activeTiles.Add(go);
 }

 private void DeleteTile()
 {
     Destroy(activeTiles[0]);
     activeTiles.RemoveAt(0);
 }

endregion

Comment
Add comment · Show 1
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 meat5000 ♦ · Apr 07, 2018 at 11:07 AM 0
Share

But what did you try with respect to your question?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Priyanka-Rajwanshi · Apr 07, 2018 at 06:52 AM

@ElaynYT Use Random.Range for randomisation as below:

 private void SpawnTile()
 {
      int prefabIndex = Random.Range(0, TilePrefabs.Length);
      GameObject go = Instantiate(TilePrefabs[prefabIndex]) as GameObject;
      go.transform.SetParent(transform);
      go.transform.position = Vector3.forward * spawnZ;
      spawnZ += tileLength;
      activeTiles.Add(go);
  }
Comment
Add comment · Show 4 · 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 ElaynYT · Apr 07, 2018 at 10:26 AM 0
Share

I Tried it but now its jumping platforms by this i mean its not spawning random platforms which i have added but the same platform but just random distance apart this makes the character fall to the void! What do i do? (P.S :- Its spawning the same platform with different distance and therefore there is a gap)

avatar image Priyanka-Rajwanshi ElaynYT · Apr 07, 2018 at 11:05 AM 0
Share

Did you debug the prefabIndex? Also, are all your prefabs of different size? Does the array contain different prefabs?

avatar image ElaynYT Priyanka-Rajwanshi · Apr 07, 2018 at 12:30 PM 0
Share

1 platform is simple but others have different obstacle placed differently and yes they are different prefabs.Not if you are speaking about the size since i have obstacles so the y axis also increases so... i don't know. And what do you mean by debug the prefabIndex

Show more comments

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

141 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 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 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 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 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 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 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

cornucopia 3d model? 0 Answers

Unity5/C# beginner here! Need some scripting help :) <3 1 Answer

"Field " " is never assigned to, and will always have it's default value null" 2 Answers

error CS0029: Cannot implicitly convert type `UnityEngine.GameObject' to `Assets.Scripts.GazeObject' 1 Answer

Can't find my Camera inside my Instantiate prefab 0 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