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 marijnroukens · Jan 22, 2015 at 09:59 AM · c#randomgenerateblocksendless

Generate new blocks to land on

Dear unity Community,

I am trying to make a sort of endless runner, and I am trying to generate the next block when you touch the current block. So it will be endless. The distance has to be random but has to have a max so it is possible to make it and not to far away.

It is in 2D.

I wrote the following script, but with this the block only comes up on 1 place. The block taken is random but now I need the distance to be correct..

 using UnityEngine;
 using System.Collections;
 
 public class createNewBlocks : MonoBehaviour {
 
     public Transform[] teleport;
     public GameObject[] prefeb;
 
     public float XPosition = 0.0f;
     public float YPosition = -2.6f;
 
 
 
     void OnCollisionEnter2D(Collision2D coll)    {
         int tele_num = Random.Range(0,8);
         int prefeb_num = Random.Range(0,2);
         XPosition = XPosition += Random.Range (1, 20);
         Debug.Log ("Xposition =" + XPosition);
         Instantiate(prefeb[prefeb_num], teleport[tele_num].position = new Vector2(XPosition,YPosition), teleport[tele_num].rotation );
 
 
     }
 }
 
 

It would be great if someone could help me, thanks in advance!

Kind Regards,

Marijn

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

1 Reply

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

Answer by cdrandin · Jan 22, 2015 at 10:09 AM

 XPosition = XPosition += Random.Range (1, 20);

is double assignment, wasteful. Should be...

 XPosition += Random.Range (1, 20); // is also XPosition = XPosition + Random.Range (1, 20);


Are you sure teleport[] list have different transform values? Print them out. Maybe all of the position are just at one point and you are always going to place the blocks in the same spot because they are all the same values.

Comment
Add comment · Show 3 · 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 marijnroukens · Jan 22, 2015 at 10:32 AM 0
Share

Oh thanks, you're right! But I am sure I have given the teleport[] diffrent transform values, they all have a unique position.

I am also getting the error "IndexOutOfRangeException", but not always. Sometimes it creates a new empty block clone, and sometimes it gives me the error above. it looks like it doesn't do anything with the prefab when it does create one?

avatar image cdrandin · Jan 22, 2015 at 08:23 PM 0
Share

It wouldn't hurt to valid your transforms. I don't know what exactly you are doing to populate your list so I can't know for certain why you are getting the exception from time to time. One idea could be you are removing from the list while iterating over it, thus co$$anonymous$$g off one short if you are removing and traversing at the same time.

avatar image marijnroukens · Jan 22, 2015 at 11:18 PM 0
Share

cdrandin, I am so sorry. You were right all along, I was to sure of myself that I didn't made any mistakes but I switched the teleport and prefab section in the editor.. So the teleport was imported to the prefab haha? Seems legit that it didn't work. Anyway thanks for the effort! Really helped me out.

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

25 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

Related Questions

Generating a word out of an array, each has its own rarity. 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to instantiate prefabs at random postions in 2d 1 Answer

How do you make a spawner block? 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