Question by
DeadLizard · Sep 28, 2015 at 06:38 AM ·
arraysfreezelooping
Unity is freezing and I cannot figure out why
I am making a card game and when I hit play unity is freezing. Could you please help me find what in my code is causing this? Thanks! using UnityEngine; using System.Collections;
public class Deck : MonoBehaviour {
public GameObject Card;
public GameObject[] Cards;
public float deckSize;
private int decklength;
public float offset;
private string[] cardNames = {"aC","2C","3C","4C","5C","6C","7C","8C","9C","10C","jC","qC","kC","aD","2D","3D","4D","5D","6D","7D",
"8D","9D","10D","jD","qD","kD","aH","2H","3H","4H","5H","6H","7H","8H","9H","10H","jH","qH","kH",
"aS","2S","3S","4S","5S","6S","7S","8S","9S","10S","jS","qS","kS"};
private float startTime;
private float journeyLength;
public float speed = 1.0f;
// Use this for initialization
void Start () {
decklength = Mathf.RoundToInt(deckSize);
Cards = new GameObject[decklength];
offset = 1 / deckSize;
for (int i = 0 ; i < decklength; i++)
{
GameObject go = Instantiate(Card,transform.position + new Vector3(0, i*offset, 0),transform.rotation) as GameObject;
Cards[i] = go;
Cards[i].transform.parent = transform;
Cards[i].name = cardNames[i];
}
Shuffle();
}
// Update is called once per frame
void Update () {
}
void Shuffle ()
{
for (int i = 0 ; i < decklength; i++)
{
GameObject tmp = Cards[i];
int r = Random.Range(i,decklength);
Cards[i] = Cards[r];
Cards[r] = tmp;
}
for (int i = 0 ; i < decklength; i++)
{
Vector3 pos1 = Cards[i].transform.position;
Vector3 pos2 = transform.position + new Vector3(0, i*offset, 0);
startTime = Time.time;
journeyLength = Vector3.Distance(pos1, pos2);
while(Cards[i].transform.position != pos2)
{
float distCovered = (Time.time - startTime) * speed;
float fracJourney = distCovered/journeyLength;
Cards[i].transform.position = Vector3.Lerp(pos1, pos2, fracJourney);
}
}
}
}
Comment
Answer by getyour411 · Sep 28, 2015 at 10:50 PM
On reading your post title, I looked for and found the keyword behind most "Unity is freezing" - which is "while". Look at your while code, it's probably in an infinite loop.
Your answer
Follow this Question
Related Questions
For Loop looping out of Loop Condition 1 Answer
Need help looping through an array. 0 Answers
Help understanding loops with arrays. 0 Answers
Unity freezes. I dont know why... 3 Answers