Object follow another object on the x axis

Heres the code one of the users posted in reply to another question:

using UnityEngine;
using System.Collections;

public class BallFollowBar : MonoBehaviour {
	
	float barPos;
	
	void start() {
		barPos = GameObject.Find("PlayerMovingBar").transform.position.x;
	}
	
	void Update() {
		Vector3 tmpPosition = transform.position;
		tmpPosition.x = barPos;
		transform.position = tmpPosition;
	}
}

Im trying to make an gameObject follow another on the x axis. The code compiles, but only the PlayerMovingBar moves. Wasnt the ball supossed to be following it?

In your Start function (with ā€˜Sā€™, not ā€˜sā€™), you copy the initial value (on startup of the application, or when the object is created) of the x-position of the object in the barPos variable. But it will not be refreshed afterwards.
So you have to refresh barPos everytime in the Update function.

This should work:

using UnityEngine;
using System.Collections;
 
public class BallFollowBar : MonoBehaviour {
 
    Transform bar;
 
    void Start() {
       bar = GameObject.Find("PlayerMovingBar").transform;
    }
 
    void Update() {
       transform.position = new Vector3(bar.position.x, transform.position.y, transform.position.z);
    }
}