• 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 LegionIsTaken · Oct 01, 2011 at 03:22 PM · crashsendmessage

SendMessage crashes Unity Editor

Hello brothers from other mothers!

I have created a barrel which should explode when some requirements are met. It should also deal some damage based on distance, but for some reason SendMessage(which sends the damage information) causes Unity to crash and I don't know why.

Here is my code if you want it:

 var health = 100.0;
 var explosion : GameObject;
 var expSounds : AudioClip[];
 var leakSounds : AudioClip[];
 var explosiveRadius = 5.0;
 var explosivePower = 100.0;
 var force = 200.0;
 var Damage = 150.0;
 var fireSpew : ParticleEmitter;
 var deadReplacement : Transform;
  
  function ApplyDamage (damage : float){
      health -= damage;
      
      if(health <= 25){
          if(fireSpew){
              fireSpew.emit = true;
              Invoke("Kill", 3);
              audio.PlayOneShot(leakSounds[Random.Range(0,leakSounds.Length)]);
          }
      }
      if(health <= 0){
          Kill();
      }
      
  }
  
  function Kill (){
      var explosionPos : Vector3 = transform.position;
         var colliders : Collider[] = Physics.OverlapSphere (explosionPos, explosiveRadius);
         
             Instantiate (explosion, transform.position, transform.rotation);
             audio.PlayClipAtPoint(expSounds[Random.Range(0,expSounds.length)], transform.position);
     
         for (var hit : Collider in colliders) {
 
             if (!hit){
                 continue;
             }
             if (hit.rigidbody){
 
                 hit.rigidbody.AddExplosionForce(explosivePower, explosionPos, explosiveRadius, 1.0);
                     var proximity = Vector3.Distance(transform.position, hit.transform.position);
                     var effect : float = 1.0 - (proximity / explosiveRadius);
                
                hit.SendMessage("ApplyDamage",Damage*effect, SendMessageOptions.DontRequireReceiver);
                }
            }
            var REP = Instantiate(deadReplacement, transform.position,transform.rotation);
      
            REP.rigidbody.AddForceAtPosition(Vector3(Random.Range(-1000,1000),2000, Random.Range(-1000,100)), transform.position);
         Destroy(gameObject);
     }

The code gives no errors and I do not have any problems/crash/bugs using SendMessage on anything else then the barrels. Even if I copy and paste my script from other objects the it still crashes Unity.

If any of you could have some idea of what is causing the crashes and possible any solutions to them I would be very thankful.

Thank you.

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
3
Best Answer

Answer by aldonaletto · Oct 01, 2011 at 04:00 PM

I suspect you may be entering a recursive loop: Kill() calls ApplyDamage in any collider intersecting the sphere, but ApplyDamage also calls Kill(), which will call ApplyDamage etc. You can try to let the exploding object's collider out of the loop using this:

 ...
 for (var hit : Collider in colliders) {
     if (hit == collider || !hit){ // skip this collider and the null ones
         continue;
     }
     if (hit.rigidbody){
         ...

Comment
Add comment · Show 2 · 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 LegionIsTaken · Oct 01, 2011 at 07:12 PM 0
Share

Oh my. I would newer have thought of that, you were right I did create a recursive loop.

Thank you aldonaletto you newer fail to amaze me!

avatar image aldonaletto · Oct 01, 2011 at 10:11 PM 0
Share

I had a similar problem some time ago trying to move the character inside a OnTrigger event (definitely a bad idea!) - that was when I learned that infinite recursive loops could crash Unity!

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

Unity crash 1 Answer

Easy collision script not working, don't understand. 2 Answers

I rendered my game and it crashes when I start it, how can I fix it? 1 Answer

pc applicaton crash after quit 1 Answer

4.1.5f1 MacOS Player crashes on startup 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