So I’m guessing it’s some kind of 2D game if you are trying to get an x position specifically not just a 3D space somewhere in your scene?
For that the transform stuff should work.
Just want to briefly mention if you are new to C# or Unity that another great way to get scripts to execute based on your player reaching a specific position is to set up an area trigger object.
You can do this by creating an empty GameObject and placing any kind of collider on the object (I usually use spheres or box).
Then you create a collider on your player object or a child of the player object. Set that collider to Is Trigger = true in the Unity Inspector. Then attach a rigid body to the same object with the trigger. For my games I always have set these to gravity disabled and Is Kinematic enabled so it doesn’t effect the game play if you aren’t using gravity on your player.
Then you can place a script on the first trigger detection collider you set up and placed in the desired area of your scene. On the script you can do something like:
GameObject newTriggerObject;
void OnTriggerEnter(Collider triggerObject)
{
if (triggerObject.name == "Player")
{
//execute your code
//you can create a reference for the triggerObject for more complicated functions like:
newTriggerObject = triggerObject.gameObject;
//access the newTriggerObjects script:
newTriggerObject.getComponent<PlayerScript>().triggerExampleFunction();
}
}
You can also do the same exact stuff for OnTriggerStay or OnTriggerExit for complete area detection functionality.
I think this would be more performance friendly than checking for the distances and conditions every Update or FixedUpdate but am not 100% sure.
With this system you can just have the area collider sitting pretty much idle until the OnTriggerEnter activates and then it can shoot off a single clean function calls or other logic stuff.
These kind of triggers can be very useful since they can be put on any collider in your game at any location. Therefore they are not dependent on specific logic based on their position in the scene. They can work exactly the same anywhere in your game.
For example my game so far is mainly flat but there are areas where you can run up hills or down slightly through shallow ponds. Because the game is mostly flat it wouldn’t make sense for me to use my rayCast system I use to determine player height 100% of the time. Instead I created a collider like my example and set this collider into the areas of my game with uneven terrain. Therefor when the player enters the collider it switches on a “elevationBool” so that the player actually uses the rayCast to determine what his height from the ground should be.
That’s just one example of how this sort of positional detection could be useful instead of always checking the transform.position every frame.
This is a little bit more than you probably need but I know I had a bit of trouble getting all this kind of stuff sorted out when I first started so I’m sure this example could be useful for yourself or others with the area detection stuff.