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 ThomasMarsh · Feb 04, 2015 at 12:44 AM · javascriptcrashontriggerenterloop

Unity freezes when OnTriggerEnter occurs

I have been attempting to write a collection of scripts that deals with player health and an enemy object hitting a player object. This script is placed in a hitbox child of the enemy object and refers to a player health script which seems to be working correctly. When I approach the enemy however, Unity freezes up completely. I know that it is most likely some sort of loop or condition that is executing infinitely with little to no delay but I cannot seem to pinpoint exactly what it is. Most of the pieces of the script are modeled off of snippets of code that I have used before that worked so I am not quite sure exactly what the issue is. Any help would be appreciated. Here is the script.

 #pragma strict
 
 public var damageDone: float = 45f;
 public var hitCoolDownTime: float = 1f;
 public var meshObject: GameObject;
 
 private var playerObject: GameObject;
 private var playerHitBox: GameObject;
 private var playerState: PlayerState;
 private var hitCoolDown: boolean = false;
 private var hitTimer: float = 0f;
 
 //Crashes the game
 
 function Awake () {
     playerObject = GameObject.FindGameObjectWithTag("Player");
     playerState = playerObject.GetComponent(PlayerState);
 }
 
 function Update () {
     if(hitCoolDown){
         hitTimer += Time.deltaTime;
         if(hitTimer >= hitCoolDownTime){
             hitCoolDown = false;
             hitTimer = 0f;
         }
     }
 }
 
 function OnTriggerEnter (other: Collider){
     if(other.gameObject == playerObject && !hitCoolDown && meshObject.GetComponent(SkinnedMeshRenderer).enabled){
         playerState.Damage(damageDone);
         hitCoolDown = true;
     }
 }

 
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

3 Replies

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

Answer by ThomasMarsh · Feb 07, 2015 at 12:02 AM

I managed to fix my own issue by modifying the code inside the OnTriggerEnter so that the object that collides with the trigger is checked first, and then the cooldown and meshrenderer are checked in an if statement nested one level below.

 function OnTriggerEnter (other: Collider){
     if(other.gameObject == playerHitBox){
         if(!hitCoolDown && meshObject.GetComponent(SkinnedMeshRenderer).enabled){
             playerState.Damage(damageDone);
             hitCoolDown = true;
         }
     }
 }

I also all the trigger events occur in a separate layer because I have other colliders on both the enemy and the player than the ones I use here. Now everything works perfectly. Thank you all for all the help.

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
1

Answer by MakeCodeNow · Feb 04, 2015 at 01:03 AM

Either meshObject or playerState is null.

Comment
Add comment · Show 5 · 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 ThomasMarsh · Feb 04, 2015 at 02:23 AM 0
Share

Neither are null I have not gotten any null error messages in console and the script also works as it should for a split second before it freezes up.

avatar image ThomasMarsh · Feb 04, 2015 at 02:26 AM 0
Share

The game works up until the point when the OnTriggerEnter occurs and then it freezes up.

avatar image MakeCodeNow · Feb 04, 2015 at 03:35 AM 0
Share

$$anonymous$$ust be an infiniteLoop in the playerState.Damage() function.

In general crash != hang. A crash ter$$anonymous$$ates the application while a hang keeps running, just stuck in one spot.

avatar image ThomasMarsh · Feb 04, 2015 at 01:05 PM 0
Share

The playerState.Damage() function contains one line of code which is playerHealth -= damageDone;

avatar image MakeCodeNow · Feb 06, 2015 at 04:48 AM 0
Share

Is playerHealth a simple field or a property? If it's a property, maybe the code inside it is the issue?

avatar image
1

Answer by alok-kr-029 · Feb 06, 2015 at 05:09 AM

I think so every time the trigger is entered the hitCoolDown = true and meanwhile in update every frame it gets updated and try running your if(hitCoolDown){ hitTimer += Time.deltaTime; if(hitTimer >= hitCoolDownTime){ hitCoolDown = false; hitTimer = 0f; } part of script which make an infinite loop and hangs your gameplay

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.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Audio Fading too fast 2 Answers

2D Jump AI Help 0 Answers

How do I reset a for loop variable??? 3 Answers

PC crash when open Monodevelop 0 Answers

Don'tDestroyOnLoad issue, please help :( 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