Calculating the right result

Hello dear Unity community,

I am very new to coding and working with Unity. So don’t blame me for very obvious things or mistakes. Thanks.

So what I was basically trying to do: I toss a coin and depending on the result the amount of points a user has is doubled or not. Let’s say I chose head and I get head → my points go from 20 to 40.

My script however does something totally weird. It makes from 1000 points 16000:

void Start ()
{
coins = 4000;
}
void headOrTail ()
{

if (my condition)
{
head = true;
tail = false;
if ((isHead && head))
{
coins = coins * 2;
}
}
else
{
head = false;
tail = true;
if ((isTail && tail))
{
coins = coins * 2;
}
}
}

I call headOrTail() in void OnMouseDown ().
I have no idea what happens :slight_smile:

First, welcome to the forums!

Second, be sure to use code tags when sharing code. It makes it a lot easier for us to read and consequently, more likely to help you find the solution. I’ve taken the liberty of reformatting your code (using my personal preferences for curly brace placement in the process) here:

void Start () {
    coins = 4000;
}

void headOrTail () {
    if (my condition) {
        head = true;
        tail = false;
        if ((isHead && head)) {
            coins = coins * 2;
        }
    } else {
        head = false;
        tail = true;
        if ((isTail && tail)) {
            coins = coins * 2;
        }
    }
}

The best I can guess is that you’re calling headOrTail() multiple times per flip. The logic in that function alone can’t be causing the problem, so you have to look outside this section of script for the error.

1 Like

Other than your head and tail bools being redundant as you are setting them to true before they are checked in an if statement, I can only assume the script is being called more than once.

1 Like

Thanks for your message! I will use the code function from now. I also assume that my headOrTail function is called several times. But I don’t know why. This is my complete Toss.script. I only left out the many variables.

public class Toss : MonoBehaviour {
 
void Start ()
    {
        startingPosition = transform.position;
        coins = 4000;
        kopf = GameObject.Find ("kopff");
        mainSlider.maxValue = coins;
    }

    void OnMouseDown ()
    {
        coin = GetComponent<Rigidbody> ();
        coin.AddForceAtPosition (Vector3.up * rotationSpeed, Vector3.right);
        float s = Random.Range (0.5f, 1f);
        Vector3 vec = new Vector3 (0, 0, Random.Range (200f, 660f));
        iTween.RotateBy (gameObject, vec, s);
        coin.useGravity = true;
    }

    void OnCollisionEnter (Collision col)
    {
        if (col.gameObject.name == "plane") {
            headortail ();
        }
    }
    void headortail ()
    {
     
        if (coin.transform.position.y > kopf.transform.position.y)
        {
            head = true;
            tail = false;
            if ((ishead && head))
                {
                coins = coins * 2;
                coinsLabel.text = ""+coins;
                }
            else {
                coins = 0;
            }
        }
        else
            {
                head = false;
                tail = true;
            if ((istail && tail)) {
                coins = coins * 2;
                coinsLabel.text = "" + coins;
            } else {
                coins = 0;            }
        }
    }

    public void chooseHead ()
    {
        ishead = true;
        removeDialog.SetActive (false);
        slider.SetActive (false);
    }

    public void chooseTail ()
    {
        istail = true;
        removeDialog.SetActive (false);
        slider.SetActive (false);
    }
}

Ah. There it is. OnCollisionEnter isn’t guaranteed to only execute once in this case. If the coin is bouncing at all, each time a part of it touches the plane, that function will execute. You’ll want to set a flag that only runs headortail if it hasn’t been called yet for that flip.

If that’s not it, make sure you don’t have the script attached to the coin object multiple times.

1 Like

I can’t follow :slight_smile: What do you mean by setting flags? Is there another way to tell my script to only run headortail on collision once? Because you are right, sometimes it bumps on the surface and thus jumps.

Sorry, ‘flag’ is another term for a boolean variable that keeps track of some activity or other. So something like this:

private bool hasFlipped = false;

void HeadOrTail() {
    if (! hasFlipped) {
        hasFlipped = true;
        /// Your existing method code here
    }
}

should ensure that the HeadOrTail method only executes once. Bear in mind, you’ll have to set that hasFlipped variable back to false before you go to do your next coin flip, then.

1 Like

It works fine. Thank you @Schneider21 !

Awesome! Happy to help!

1 Like

And I am happy the community here is so awesome! I bet I will need more help soon. However I am confident in succeeding with the project :slight_smile: