Counting

I’m making a 3d coin flipping game and am getting stuck trying to get my counter to count how many times the coin has landed on each side and the total flips, it will continuously count when the coin is head’s up, but nothing happens when the coin is tail’s up.
Any help would really be appreciated

using
 UnityEngine;
using UnityEngine.UI;

public class ScoreTracker : MonoBehaviour
{

    public int totalFLips;
    public int heads;
    public int tails;
    public Transform coin;
    public Text flipText;
    public Text flipCount;
    public bool landed;

      
    // Start is called before the first frame update
    void Start()
    {
        totalFLips = 0;
        heads = 0;
        tails = 0;
    }

    // Update is called once per frame
    void Update()
    {
       

        flipText.text = coin.transform.position.y.ToString("0");
        //flipCount.text = coin.rotation.eulerAngles.x.ToString("0");    // !!!the landed bool works as intended if this is commented out!!!

        int.Parse(flipText.text);
        int.Parse(flipCount.text);

        if (flipCount.text == "0")
        {
            flipTotal();
        }

        else
        {
            flipTotal();
        }

      

        //Debug.Log(flipText);
    }

    void flipTotal()
    {

        if (flipCount.text == "0") //if coin is on the ground lanned is set to true
        {
            landed = true;
        }

        else //if coin is not on ground Landed is false
           
        {
            landed = false;
        }

        if (landed & coin.rotation.eulerAngles.x == 90)   //This counts up continuously when the coin is head up 
        {
            heads++;
            Debug.Log("Heads");


        }
        if (landed & coin.rotation.eulerAngles.x == -90)    //This does not count and i do not understand why 
        {
            tails++;
            Debug.Log("Tails");

        }
    }

   
}

Remember, Update() runs every frame.

Here’s how to debug the code above:

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

1 Like

Me personally, I would add a collider to the coin prefab, then use OnCollisionEnter to determine when it landed, and OnCollisionExit to determine when it’s airborne. Then, use the eulerangle of the prefab at the collision enter to determine if it’s heads or tails and increment the corresponding int.

1 Like
if (flipCount.text == "0")
{
    flipTotal();
} else
{
    flipTotal();
}

Consider what this code is saying and what it does. If the “flipCount” is zero, run flipTotal. Otherwise, run flipTotal. Think about what you actually want to do and then adjust the code to match your intentions.

3 Likes

I considered that but i dont want the bool to be true unless the coin is flat on the ground would a collider still work for that?

I Tried taking the else statement out but my bool doesn’t work if i do, IDK why this happens, but i really appreciate the help

You don’t need an ‘if’ statement at all there. Really think logically about what that statement does.

{ DoThing() }
if (something doesnt equal something)
{ DoThing() }```

You could instead remove the 'if' all together and just

```DoThing()```

[quote="mony368, post:5, topic: 875212, username:mony368"]
I considered that but i dont want the bool to be true unless the coin is flat on the ground would a collider still work for that?
[/quote]

Something like --

```csharp
public bool touchedTable;
public bool isLanded;

public OnCollisionEnter()
{
     touchingTable = true;
}
public void Update()
{
     if (this.transform.rotation == Quaternion.Identity && touchingTable)
{
     isLanded = true;
}

public OnCollisionExit()
{
     touchingTable = false;
     isLanded = false;
}

Now my PC is rendering some animations at the moment so I can’t load Unity to test that script, but with a few adjustments that should get it headed in the right direction. Also, disclaimer, I’ve only been using Unity for about two years, and some very simple things still escape me, but I do manage to get things running most of the time.

–Edit I forgot to set the isLanded bool back to false. It’s fixed now.