• 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 Max-Dep · Nov 10, 2014 at 04:17 AM · filter

stack overflow exception

Hello,

I am doing an app in which I have to simulate and explain light's behavior when it hits various objects: Filters, Mirrors, Prisms... I have one generic script for Filter, one for Mirror, one for prism... the script is instantiated as many times are those objects are duplicated.

I am using raycast to simulate the light beam. the beam is casted from source. On raycasthit(when the beam hits the object), light information is transmitted using sendMessage to the script attached to hit object.The script will use this information to cast a new ray and so on ...

I have no problem for the mirror script. But the filter script has a problem: I am getting a stack overflow exception message and I can't figure out why.

 function HitByRaycast(receivedLightInfo) {
 
     // Retrieving Light Info
     var incomingVec = receivedLightInfo[0];
     var hit = receivedLightInfo[1];
     var inColor = receivedLightInfo[2];
     
     //Specifying Filter's color property
     var filterColor =renderer.material.GetColor ("_Color");   
     var outColor = inColor - (Color.white- Color(filterColor.r , filterColor.g , filterColor.b, 1));
     
     //building the reflection beam
     var reflectVec = Vector3.Reflect(incomingVec, hit.normal);    
     var reflectHit: RaycastHit;
     var reflectRay = Ray(hit.point, reflectVec);
     var reflectRayCast = Physics.Raycast(reflectRay, reflectHit,1000);
     
     
     //displaying the filtered light beam
     if (reflectRayCast != true)
             {
             Debug.DrawRay(hit.point, reflectVec*1000, outColor);
             }
     else if (reflectRayCast==true)
             {
             Debug.DrawLine(hit.point, reflectHit.point, outColor);
             //passing light information to the hit object
             var lightInfo = new Array (reflectVec, reflectHit, outColor);
             reflectHit.transform.SendMessage("HitByRaycast",lightInfo,
                               SendMessageOptions.DontRequireReceiver);
             
             }
     
 }
 


alt text

screen shot 2014-11-10 at 10.57.33 am.png (156.0 kB)
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

2 Replies

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

Answer by Owen-Reynolds · Nov 10, 2014 at 06:03 AM

The error means you're getting an infinite loop (a standard infinite loop merely freezes. If you have function calls, you run out of space and get Stack Overflow instead.)

You're probably getting a circle of bounces, or an object is hitting and casting from itself over and over.

Adding Debug.Log("hit "+transform.name)' can help, but debugs don't always get printed on crashes. A common trick is to throw in a bounce counter, and quit at 20 or so.

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 Max-Dep · Nov 10, 2014 at 01:19 PM

My the filter object is just a quad, so at first glance, it was a bit hard to imagine that the object could raycasthit itself. But you pointed out something right and that's indeed what is happening. I am trying to solve the issue by offsetting slightly the the new raycast origin from the raycasthit.

:) Thanks

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.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

iphone and bilinear filtering? 2 Answers

How do I change the cutoff frequency in the highpass filter? 0 Answers

Can I customize the list of labels (tags) for assets in my scene? 2 Answers

Search gameobject by tag and filter box. 0 Answers

Low pass filter on iphone accelerometer. 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