Why does this work?

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")
        {
            landed = true;
        }
        else (flipCount.text != "0")
        {
            landed= false;
        }

        if (landed  & coin.rotation.x == 90)
        {
            heads ++;
            Debug.Log("Heads");

        }
        else if (landed  & coin.rotation.x == -90)
        {
            tails ++;
            Debug.Log("Tails");
        }

        //Debug.Log(flipText);
    }

    void flipTotal()
    {
        totalFLips += 1;
    }

 
}

 
}

Hello, pretty new still, and am making a 3d coin flipping game. for some reason the bool I’m using to tell if my coin has landed only works when I comment out the 31st line. if I uncomment it the cool turns on and off as the coin spins.

You can always edit the initial post.
Also use code tags for large pieces of code. It is more readable that way.
Now you’ve lost all formatting, no line numbers just a wall of text.

It is also on the toolbar. Pick the left one for large blocks of code. The right one is for small lines.
7958172--1019847--upload_2022-3-12_2-0-7.png

Code Block:

public class Foo
{
    public void Bar()
    {
 
    }
}

Inline:
var number = 1.23456f

2 Likes

Sorry about that, thanks for the info

Welcome to the forums!

I cant tell you why it works when you comment that line out. This is mostly due to me not knowing what exactly you mean with “working”. Since line 30 controls your logic for whether the coin is landed or not, i imagine when you comment it out the bool wont ever change. Which to me cant be called “working” :slight_smile:

There is a lot you can improve with that code. From actual game breaking problems, to conveniences and conventions to make your life easier. All of this is fine of course, since you are still learning. Here we go!

Rotations in Unity are stored as Quaterions. A quaternion is a four dimensional construct in the complex number space, which is for certain reasons really good at handling rotations in 3D space. It is for these reasons used in computer graphics and… quantum physics. Needless to say, Quaternions are pretty darn complex, and i would say that only a hand full of people globally actually know on a fundamental level how its x, y, z and w component relate to each other. The point here is that rotation.x is not what you think it is, and comparing to or working with it directly is usually not intended. When you need to work with actual rotations, you dont need to know how Quaternions work, as Unity provides a bunch of functions you can use: Unity - Scripting API: Quaternion
When you need the rotation in euler angles you can simply use rotation.eulerAngles.x and so forth.

Due to floating point imprecisions it is usually not a good idea to compare floats using ‘==’. Internally the float will rarely reach a precise number, and will rather often end up at 0.0001 or 89.999998. You thus want to know if two floating point values are approximately the same: Unity - Scripting API: Mathf.Approximately
As for your flipping mechanic, you said that if you comment in line 30, it goes from true to false while spinning. While we already went over rotation.x being the wrong thing to use here, a spinning coin might inevitably at some point of its spin reach the rotational value you intend to check for. So just checking the rotation to determine whether it has landed is not good enough. I dont know how you spin the coin.
If it’s physics, you can simply check the velocity and altitude over the terrain to determine whether it landed.

That’s it about the more critical problems. Let’s get to the conveniences and conventions!

You never need to compare bools to true or false. If we have someBool, then someBool == true is the same as simply writing someBool. Both statements evaluate to true only if someBool is true. If you want to compare to false, then instead of writing someBool == false you can write !someBool (read: not someBool) instead. This is shorter and less prone to mistakes.

You do not need to use else if, if no other outcomes are possible anyways. If you check if (variable == 0), then if that is false, that already means that variable must be not equal to 0, thus you can simply write else instad of else if (variable != 0).
Again this simply improves readability and reduces code complexity, and with that the places to introduce bugs.

Last but certainly least, we have a shortcut for writing +=1 specifically, you can simply write ++ instead.

Hope this helps :slight_smile:

For the curious mind, a really good visual representation of what a Quaternion is.

1 Like

Whatever this .x value is, it’s NOT what you think it is!

The internal x,y,z,w components of a Quaternion should be treated like magic and never touched.

The internal x,y,z,w components are NOT angles, NOT degrees. Stay away from them.

If you seek angles, the .eulerAngles field can give you those, but they are of course subject to gimbal lock, which is why we use quaternions in the first place.

https://starmanta.gitbooks.io/unitytipsredux/content/second-question.html

awesome! thanks for the info I made the changes you suggested, but I think I explained wrong. The bool I’m using works if I comment that line of code out, and doesn’t work if that line is not a comment. i have updated the code in my post

thanks ill give it a watch