Multiple objects same Script

I know the question have been asked Many times but how to attach a script to multiple objects where the script works individually for each object


that is my code here for the objects , the player here is just a ball that moves in the z-direction with a certain force and all i want simply is for the objects to move whenever the player passes them .

Use code tags to paste code, not screenshots. Screenshots are a pain to read.

When you say:

What do you mean by that? Every script always works “individually for each object”, and the only reason it might not would be if you were referencing the same global values or the same object on the different copies of the script.

public class PileMovement : MonoBehaviour
{
    public GameObject player;
    public GameObject capsule;
    private float derivative;
    private float sinFunc;
    public float pile_MovementDistance;
  //  public static float targetPosition;
    float magnitude;

    private void Update()
    {
    //    targetPosition = pile_MovementDistance;
       // Debug.Log(this.transform.position.z);
        if (player.transform.position.z > transform.position.z)
        {
            Debug.Log(transform.position.z);
            float angleCos;
            angleCos = Mathf.Cos(Accelerator.angleDerivative);

            transform.gameObject.GetComponent<Rigidbody>().MovePosition(transform.position + Vector3.forward * 100);
            //capsule.GetComponent<Rigidbody>().MovePosition(transform.position + Vector3.forward * Accelerator.ballSpeedVector * angleCos);

        }


    }

thats the code for you if you couldnt read it ,i didnt get what you meant by "The same object on different copies of the script "

I guess we’re just not understanding what each other is trying to say?

Let’s try it this way: What is the specific problem you have? When you try and do whatever you’re trying to do: What is the result you expect, and what is the result you see instead?

I have an object pool that instantiates objects along the Z-Axis ,each time the ball passes an object i want that object to move but that doesnt seem to happen ,

What happens instead? Do none of them ever move, or do they move at the wrong time, etc?

Do they all have the “player” variable correctly assigned?

  private void Update()
    {
    //    targetPosition = pile_MovementDistance;
       // Debug.Log(this.transform.position.z);
        if (player.transform.position.z > transform.position.z)
        {
            Debug.Log(transform.position.z);
            float angleCos;
            angleCos = Mathf.Cos(Accelerator.angleDerivative);

            transform.gameObject.GetComponent<Rigidbody>().MovePosition(transform.position + Vector3.forward * 100);
            //capsule.GetComponent<Rigidbody>().MovePosition(transform.position + Vector3.forward * Accelerator.ballSpeedVector * angleCos);

        }


    }

the if function is always false , thats why the object doesnt move ,when i debug the transform position in z it is always the position of the last instantiated object