Yay! I had my collectables, my randomly generated shopping list, and my working raycast directed projectile. But I needed to keep track of items in the cart and compare them to the shopping list, then ultimately communicate that to the player through UI.
I thought it would be easy...
Ultimately, I would love the shopping list to be a piece of notebook paper with hand-written text. Each time you find something, it should "scratch" that item off the list. (Of course, my modern shopping experience is that I make shopping lists on my Phone and click a box to mark it off...hmmm...something to consider.) But for now, I just wanted the text to change color (to green) when the correct item fell into the cart.
Changing the font color was overly complicated. That's because I have two different scripts, one managing the shopping list and the other reading what items are in the cart. So I have to use .GetComponent<> quite often to reference lists, objects, properties, and call functions. Maybe I'll combine the two scripts to make things simpler.
But for now...
PSEUDO CODE!:
-When a GAME OBJECT enters the shopping cart box trigger:
-Check if that GAME OBJECT is tagged as a "Product".
-If yes:
-Get items script with properties
-Get item's "cost" property from that script.
-Add that GAME OBJECT to the list "Items in the Cart"
-Add that GAME OBJECT to the list "Items in Basket"
-Update List
-Update Cost
In actual code:
Update list is actually in another script. It's what I'm using to check whether or not the player has found an item on their shipping list. If what is in the cart matches what's on the list, it changes the font color to green. Because I was hopping between scripts so much, I got confused at first and was trying to change the font color of a GameObject instead of a UI Text Object.
...I'm smarter now...
Here's the list updating in action!
I just want to say that this little demonstration, as simple and mundane as it may appear, was a rough week for me!
Then I found that all Color elements have an Alpha Layer Property (Transparency), and for some reason its default is 100% transparent. So when I turned it 100% opaque--guess what, there was my text!
But then I ran into another problem. My script was written so that when an object fell into the shopping cart, a function compared all items in the car to all items on the list. If a match was found, then the relevant UI element turned green (as demonstrated). But they wouldn't change back again once the object was removed.
I tried a few variations of if-else statements, but I kept running into the problem of turning all the text back to black.
I'll try to explain my mistake with pseudo code:
When Update List is Called--
Cycle through each item on the shopping list
For each item in the shopping cart, compare its Name to that of the current Shopping list item.
If they match, turn its text to green.
If they don't match, turn its text to black.
I was trying to turn the text black when an object fell out of the cart--but instead, it turned everything black. That's because when I checked for a Red item against a Red item, it turned the "Red" font green. But then it moved to the next object (let's say blue), and would check that blue object against the red. When it didn't match it would turn the "Red" font back to black.
After a few days and no immediately answers on Unity's forums, I ended up creating a Bool called "nowGreen". For each item in the list, I set the nowGreen to false. As the function checks each item, there's an else-if statement that only runs if nowGreen is false, which turns the text black. If the items match, it turns the font green and changes nowGreen to True--which prevents the line of code turning the font black from ever running.
So far it's worked well so far!
My next post should be a little shorter. It's about how we do a clean up on aisle 7. I'll explain how I finally received some amazing help on the Unity forums, as well as, realized my game is too easy!
If you made it this far, how about leaving a comment so I know you were here?


No comments:
Post a Comment