- Home /
Why is this code crashing Unity?
I have an OnPointerClick() function that calls a few codes, but crashes unity once it reaches this:
while (true)
{
rectTransform.anchoredPosition = Input.mousePosition;
}
(simplified of course^) It's not the while loop crashing it, nor is it the position = mousePosition code. It only crashes when they're together. I don't get how to fix this. I'm trying to create a system where you can click an object, and drag it to another slot in my inventory UI.
Answer by TomatJuice · Aug 07, 2020 at 09:03 PM
If a while statement doesn't have time to breath then it will take up all the memory and crash unity so if your going to use a while loop make sure you can break out of it, or you can place that code in a courtine and use wait for seconds to stop the crash.
I have it set to break; on Input.Get$$anonymous$$ouseDown(0), and yet it still crashes.
By break I meant that to use a while loop you need an exit from it
Ahh right, right. I see where I went wrong. I got so used to using Unity's Update() that I simply forgot not all code processes frame-by-frame and will eat up memory to execute. Thank you!
Also judging by your code you don't thw while loop. Just placed the piece of code in update or any method being called by update
I've tried the coroutine, but I didn't try the WaitForSeconds. I'll try that.
Your answer
Follow this Question
Related Questions
Array index is out of range? 1 Answer
How to go around creating inventory with new GUI (4.6 Beta)? 1 Answer
How can I display a ScriptableObject as a list inGame? 1 Answer
GameObject blocking OnPointerEnter 1 Answer
Access to slot Item 0 Answers