• 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 30crlh · Dec 01, 2015 at 06:20 AM · raycasthit2dif statementcrashesloops

Infinite loop being caused by instantiating through an if statement

This is a hard one and I am having a tough time just formulating the question. Sorry in advance for the bad looking code and for possible noob mistakes, I am mostly self-taught.

I'm building a terrain generator based on 2D blocks. The expected behaviour is that I'm placing a column of blocks and then each one of the blocks has a script based on if statements to automate the creation of a hill like structure. So each one of the blocks checks if it has an upper block, then a right and a left block. If an upper block exists and no right or no left block exist then a right or a left block is created. I am doing this using raycasthits, if the raycast hits a collider then it knows there is a block.

What is hapenning right now is that I cannot for the life of me get rid of a loop that is creating left and right blocks infinitely despite the fact that there already is a left or a right block. It should test the neighboorhood and check that a block already exists and don't do anything, but instead it is just creating blocks with no respect whatsoever to the if statements

Here is the script code

using UnityEngine; using System.Collections;

public class CornerBlockInstance : MonoBehaviour {

     public BoxCollider2D block;
     RaycastHit2D uhit;
     RaycastHit2D lhit;
     RaycastHit2D rhit;
 
     void Start () {
         TerrainGeneration (); 
     }
 
     void TerrainGeneration() {
 
         uhit = Physics2D.Raycast (new Vector2 (transform.position.x + 0.5f, transform.position.y + 1.1f), new Vector2 (0, 0.5f), 0.5f);
 
         lhit = Physics2D.Raycast (new Vector2 (transform.position.x - 0.1f, transform.position.y + 0.5f), new Vector2 (-0.5f, 0), 0.5f);
 
         rhit = Physics2D.Raycast (new Vector2 (transform.position.x + 1.1f, transform.position.y + 0.5f), new Vector2 (0.5f, 0), 0.5f);
 
         Debug.DrawRay (new Vector2 (transform.position.x + 0.5f, transform.position.y + 1.1f), new Vector2 (0, 0.5f), Color.blue, 200);
         Debug.DrawRay (new Vector2 (transform.position.x - 0.1f, transform.position.y + 0.5f), new Vector2 (-0.5f, 0), Color.blue, 200);
         Debug.DrawRay (new Vector2 (transform.position.x + 1.1f, transform.position.y + 0.5f), new Vector2 (0.5f, 0), Color.blue, 200);
 
         if (uhit) {
             if (lhit != null) {
 
                 BoxCollider2D heightBlock; 
                 heightBlock = Instantiate (block, new Vector2 (transform.position.x - 1f, transform.position.y), transform.rotation) as BoxCollider2D;
                 Debug.Log (lhit.transform.position);
             }
 
             if (rhit == null) {
 
                 BoxCollider2D heightBlock1; //instancia um bloco
                 heightBlock1 = Instantiate (block, new Vector2 (transform.position.x + 1f, transform.position.y), transform.rotation) as BoxCollider2D;
                 Debug.Log (rhit.transform.position);
         }
     }
 }

 

I am not sure where the problem resides in my code but I think it might have something to do with the if statements and the fact that I am checking for a null raycast hit.

Thanks in advance.

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 ElDo · Dec 01, 2015 at 07:15 AM 0
Share

I'm not sure on this because I usually work in 3d. Is there any Reason why you enter 0.5 in your directions ins$$anonymous$$d of 1? Can't you do your Raycast from the Center of your Objects? Do you ever reach a Frame or something? because if there's never any other object allready in the Scene when starting this it will create into infinity because there will never be anything to collide with. $$anonymous$$aybe you Need to create Limitation blocks on each side you build your Terrain onto.

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

[RESOLVED] Error? Black Screen! I can not move anything or click on anything! pls Help! 0 Answers

Unity Crash Report doesn't work 0 Answers

For loop not working 1 Answer

trying to instantiate an object a random number of times ,how do i spawn something in a random ammount of times? 0 Answers

Delete duplicates in a list of vectors 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