The question is answered, right answer was accepted
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);
}
}
}
@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.
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.
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
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??
@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
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