|
I made a simple inventory system where you can drag and drop items inside the inventory screen. I have an array of GameObjects (items) and an array of Rect (rects) both the same size (32). "items" is used to access the inventory texture to use of each item, and "rects" is used to place a rectangle on that texture to detect mouse input. Here is the relevant code :
Everything works well, but if I try to select the last item that wasn't dragged, Unity crashes. So if I have 1 item in the inventory and I select it, it crashes. If I have 3 items, I can move any 2 items but moving a third one causes a crash. Commenting the lines 1, 2 and 3 (see commented numbers in code above) prevents the crash from happening, but I obviously need those lines. I hope I gave enough information. This problem caused me numerous headaches, so thanks for any help.
(comments are locked)
|
|
I don't see anything specific that is a bug, but here is a bunch of random thought I had on how I would debug this. :) Maybe it will trigger some useful ideas. At first it sounded like a fencepost, or off-by-one error, where you were using zero-based arrays as one-based arrays. But - you're checking if the array entry exists before using it, so that's out. But that leads me to ask if the array entry is valid - existence just means it isn't null. Maybe in some other part of the code, you Destroyed an object without removing it from the array? Or maybe it isn't fully created/initialized yet? And I would re-write the code to assign both the GameObject and Component to temporary variables - both to avoid re-finding them multiple times, and to make it easier to debug. Minor point - I'm not sure if your array is holding GameObjects, or Transforms (and as a Unity noob, I still get a bit hazy on those :) So this code may not be exactly correct. But try this code, and double-check everything for validity:
Another preference of mine, is to check for item validity at the top of a loop or function, and doing a continue/return if something is invalid. This avoids nesting code, deeper and deeper.
(comments are locked)
|
|
In your first if-block within the loop, you have this
Do you mean
instead of
Also, maybe you should consider just calculating an offset like this
So the code could look cleaner (and helps to avoid typo) Thanks, I didn't notice the typo, but it didn't resolve my problem unfortunately.
May 27 '10 at 12:49 PM
Jeremy 2
(comments are locked)
|
