Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Mar 19, 2021 at 03:44 PM by CrispGalileo for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by CrispGalileo · Mar 19, 2021 at 03:31 PM · raycastprefabphysics2dstackoverflow

Getting StackOverflowException from raycasts on a prefab object

I have this setup for collision detection:

 public class FishController : MonoBehaviour
 {
     private Rigidbody2D rb;
     private BoxCollider2D boxCollider;
     public LayerMask collisionLayer;
 
     public float moveSpeed;
     public float raycastDistance;
 
     private Vector2 direction;
 
     private void Awake()
     {
         rb = gameObject.GetComponent<Rigidbody2D>();
         boxCollider = gameObject.GetComponent<BoxCollider2D>();
         ChooseTargetLocation();
     }
     private void FixedUpdate()
     {
         CheckForObstacle();
         rb.MovePosition(rb.position + (direction.normalized * moveSpeed * Time.deltaTime));
     }
 
     private void ChooseTargetLocation()
     {
         direction = Random.insideUnitCircle;
         RaycastHit2D hit = Physics2D.Raycast(rb.position, direction, raycastDistance, collisionLayer);
         if (hit.collider != null)
         {
             ChooseTargetLocation();
         }
     }
 
     private void CheckForObstacle()
     {
         RaycastHit2D hit = Physics2D.Raycast(rb.position, direction, raycastDistance, collisionLayer);
         Debug.DrawRay(rb.position, direction.normalized * raycastDistance, Color.red);
         if (hit.collider != null)
         {
             Debug.Log("hit");
             ChooseTargetLocation();
         }
     }
 }

This was working fine, up until I turned the gameObject holding FishController into a prefab. Suddenly, the moment I hit play, I get a StackOverflowException.

FishController.ChooseTargetLocation () (at Assets/Scripts/FishController.cs:31) FishController.ChooseTargetLocation () (at Assets/Scripts/FishController.cs:34) FishController.ChooseTargetLocation () (at Assets/Scripts/FishController.cs:34) ...

And line 34 runs forever. This even happens when I remove any object from the collisionLayer. There is nothing the ray could be hitting. Anyone know whats going on here?

Comment
Add comment · Show 2
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 toficofi · Mar 19, 2021 at 03:41 PM 0
Share

Could it be hitting its own collider?

avatar image CrispGalileo toficofi · Mar 19, 2021 at 03:43 PM 0
Share

Lol yep. I'm dumb, really should've been able to figure that one out. Thanks!

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

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

Can't find the null reference 1 Answer

How to specify which version of a method to use 0 Answers

Instantiate a prefab and parenting it? 1 Answer

How to rotate a player around the corner of a box? 0 Answers

Destroying a prefab with a prefab? 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