Score Points based on Animating Png Texture
Hi.... I have a 3d object and I have it changing(animating) png textures from 100 pts to 200 pts to 300 pts, etc. When I touch the 3d Object I want it to score whatever the png texture is at that moment... so if it is animating and you touch 300 pts... Boom 300 pts is added to score... Wondering if anybody has any ideas on how to make this happen... Code is in c-sharp... Thanks...
Answer by Alec-Slayden · May 25, 2016 at 07:51 AM
While it would help to see the code, I assume the score object has a monobehaviour on it that is changing the PNG. I would add an integer that represent the score to that script. When the script changes the texture, it should also change that integer.
If this is the only object in your scene that does this, then your score script can reference the object in the inspector and you can make that integer public, for simplicity.
Otherwise, if you have a lot of these and so can't be sure which one was clicked, there are a couple options. For all of them, I recommend using an EventTrigger component on the object, with a pointer click event that targets a method on that same script (make sure you have a unity event system with touch / mouse event components as required, and a physics raycaster on the appropriate camera)
From there, you have a few options. Lets say the name of the actual score calculation script is "scorekeeper", and lets say the animated objects are "bonus items": 1) Have a public static method in your scorekeeper script that the bonus items can call when they are clicked, sending their private integer as a parameter
2) Implement your scorekeeper as a singleton and have the bonus items call a public method on that instance when they are clicked. This is kind of an overkill extension of the implementation of 1. You should avoid it unless you need to access the scorekeeper in a lot of ways all over the place or you have to ensure for some reason that there is only one.
3) Use a static EventHandler on the bonus item script that is published when a bonus item is clicked. Have your scorekeeper subscribe to that event handler. This is particularly good if you need other things to be able to listen to what bonus item was clicked, however it requires extra care in cleaning up your subscriptions. It's worth it if you're comfortable with C# events.
4) Find the scorekeeper in the bonus items awake, using FindObjectOfType and store it in a private field. Then call a public method when the bonus item is clicked. I recommend against this method since I never like Find use (it's slow), and managerial references are not usually the best option in my opinion (they feel like they go against code flow).
I'm sure there are more and potentially better options, but those are the ones that come immediately to mind that let you keep the bonus items as self-encapsulated prefabs
Answer by fredmiller12345 · May 25, 2016 at 05:49 PM
Wow! Thanks for your detailed answer... Let me digest it and reply back.... Thanks again... Fred
Hi Alec... Just wondering if I might be able to share my code with you through email... If so, my email is ravenger1@aol.com... If not... No prob... Best regard,,, Fred