Why does this script make unity crash?
using UnityEngine; using System.Collections; //THIS SCRIPT MAKES UNITY CRASH-------------FIGURE OUT WHY public class weaponProjectileInst : MonoBehaviour { public bool hasntHitAnything = true; //condition for while loop public float bulletSpeedValue; public float bulletDropValue;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
while (hasntHitAnything == true)//code for bullet drop and stuff here
{
transform.Translate(Vector3.forward * Time.deltaTime * bulletSpeedValue);//Move Forward
transform.Translate(Vector3.down * Time.deltaTime * bulletDropValue);//bullet drop
}
}
void OnTriggerEnter(Collider other)
{
hasntHitAnything = false;
Destroy(this);
}
}
so basically i was starting out with some code for a projectile script, but everysingle time it makes unity crash
Answer by jgodfrey · Mar 29, 2016 at 12:06 AM
It's a problem because you have an infinite loop. First, you set your hasntHitAnything bool to true. Then, Unity enters the Update method on the first frame. There, as long as the bool is true, you continue to loop. That loop will never exit as you're never giving anything else a chance to run (including your OnTriggerEnter method) - it just loops there forever.
You need to remove that while loop.
Your answer
Follow this Question
Related Questions
Inactive in console even though its active in the inspector. 0 Answers
NullReferenceException: Object reference not set to an instance of an object 0 Answers
how do i fix all compiler errors so i can enter playmode? 4 Answers
[RESOLVED] Error? Black Screen! I can not move anything or click on anything! pls Help! 0 Answers