Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Feb 15, 2018 at 01:18 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by plumel · Mar 09, 2016 at 03:26 PM · collisioncrashcollider2dontriggerexit

Unity crash on collision

Hello, My unity keeps crashing when the shuriken prefab collides with the box collider 2D prefab on my background.

It happened after I implented this while loop which I'm not confident on, so any help is appreciated..

 private Rigidbody2D rb2d;
 public float speed;
 public GameObject Shuriken;
 private int livess = 3; 
 void Start ()
 {
     rb2d = GetComponent<Rigidbody2D> ();
 }
 void Update () 
 {
     rb2d.AddForce(Vector2.left*speed);
 }
 void SpawnShuriken()
 {
     Vector3 RandomSpawn = new Vector3 (Random.Range (10.07F, 10.07F), Random.Range (-4.07F, 4.07F),1F);
     Instantiate (Shuriken, RandomSpawn, Quaternion.identity);
 }
 void OnTriggerEnter2D(Collider2D other)
 {
     while (livess>0)
     {    
         if (other.gameObject.CompareTag ("Player")) 
         {
             if (livess <= 0) 
             {
                 Destroy (GameObject.FindWithTag ("Player"));            
             } 
             else 
             {
                 livess = livess-1;
                 Debug.LogError (livess);
                 break;
             }
         }
     }
 }
 void OnTriggerExit(Collider2D other)
 {
     if(other.gameObject.CompareTag ("Background"))
     {
         SpawnShuriken ();
         Destroy (Shuriken);
     }
 }

}

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 plumel · Mar 10, 2016 at 04:13 PM 0
Share

@Salmjak Are you able to help with this at all? It's almost the same issue as last time, but after I added the while loop (I though this might make it loops through all the options and breaks out when) it crashes when it hits the boxcollider2d attatched to the background tag and gameobject.

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Salmjak · Mar 10, 2016 at 05:35 PM

@plumel You have an infinite loop. Your application probably don't crash, but freezes due to being stuck in that while-loop.

We already know(?) that livess won't go below 2 (strangely enough). So the loop just continues on and on and on.

Have you tried changing private int livess = 3; to public int livess = 3; ? To see if thats the cause of livess not decreasing.

EDIT: Might clarify. if (other.gameObject.CompareTag ("Player")) this is never true for the player (since other will be anything but the player, since this script is on the player). Thus you have an infinite loop.

Comment
Add comment · Show 3 · 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 plumel · Mar 10, 2016 at 05:56 PM 0
Share

Shouldn't the break line take it out of the loop? And I'll try changing it from private to public after I figure this out, thanks again

avatar image plumel · Mar 10, 2016 at 06:02 PM 0
Share

I HAVE FIXED IT - the loop that is. I took out the while loop and put in "Return;" ins$$anonymous$$d of break;. AND PUBLIC HAS FIXED THE LIVESS. WHAT!

So lives now goes down to 0. However, I'm getting this error now: Destroying assets is not permitted to avoid data loss. If you really want to remove an asset use DestroyImmediate (theObject, true); UnityEngine.Object:Destroy(Object) Shuriken$$anonymous$$ovement:OnTriggerExit2D(Collider2D) (at Assets/Scripts/Shuriken$$anonymous$$ovement.cs:44)

I believe this is to do with the Player gameObject not being a prefab??

avatar image plumel · Mar 10, 2016 at 08:21 PM 0
Share

@Salmjak !!!! IT WOR$$anonymous$$S. THE PLAYER GA$$anonymous$$E OBJECT GETS $$anonymous$$ETED. I DON'T $$anonymous$$NOW WHAT I DID BUT IT WOR$$anonymous$$ED. THAN$$anonymous$$S FOR EVERYTHING

Follow this Question

Answers Answers and Comments

60 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

Related Questions

Crash detect 0 Answers

How can i store all colliders of colliders which collide with a prefab? 1 Answer

Issue with colliders,Newbie question about OnCollisionEnter2D 0 Answers

Why Object2D jump out collision? 1 Answer

my colliders don't work properly 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