# Setting Value based on transform position in C#

Hello all,

`````` I am new to Unity and Visual Studio and I found a tutorial on Youtube to create a simple slot machine by Alexander Zotov. I have verified that I have my code correct though my game object sizes are a little different. I am using the following code in C#:

if (transform.position.y == -3.5f)
stopSlot = "Diamond";
else if (transform.position.y == -2.9f)
stopSlot = "Crown";
else if (transform.position.y == -2.5f)
stopSlot = "Melon";
else if (transform.position.y == -1.7f)
stopSlot = "Bar";
else if (transform.position.y == -1.1f)
stopSlot = "Seven";
else if (transform.position.y == -0.5f)
stopSlot = "Cherry";
else if (transform.position.y == .1f)
stopSlot = "Lemon";
else if (transform.position.y == .7f)
stopSlot = "Diamond";

When I run the game, sometimes the code assigns the correct value to my stopSlot variable and sometimes it doesn't. It is never consistent and I've checked the final slot positions and they all agree with my tranform.position.y values listed above. I wonder if it is trying to set the variable before the slot stops but I can't be sure.

Any assistance would be appreciated. Let me know if you need me to post the entire code.
``````

Related problems:

``````0.1 + 0.2 == 0.3  ->  false
``````
``````0.1 + 0.2         ->  0.30000000000000004
``````

The issue has to do with floats sometimes not being exactly the value you want due to how it is stored (in binary?) on a computer.

You could do this:

``````if (Mathf.Approximately(1.0f, 10.0f / 10.0f))
{
print("The values are approximately the same");
}
``````

Thank you, linenum,

`````` I will definitely give that a shot. How would I put that into a statement with my transform.position?

Should I assign transform.position.y to a float variable? For example, if I wanted to check the position for -0.7f, should I write something like.
``````

private float positionVariable = transform.position.y;

if (Mathf.Approximately(floatVariable, -35.0f / 10.0f))
stopSLot = “Diamond”;

Or can I use Approximately directly with the transform.position.y statement?

Thank you again

You could try this function:

``````bool yPosIsApprox(float y)
{
return Mathf.Approximately(transform.position.y, y);
}
``````