Multiple Objects Using The Same Script.

Hello all,

I have spent a good deal of time scouring this site, google, etc. for the answer to my question and have been unable to find the solution.

He is the issue, similar to others, I created a script, that when you click the left mouse button it utilized Transform.Translate(Vector3.forward * movementSpeed * Time.deltaTime,Camera.main.transform); where movementSpeed is a simple variable containing a float.

I then added this script (which uses NO Static Variables) to a GameObject, in this case a simple cube.

Everything was going along smoothly. Then, I right clicked the Cube and clicked Duplicate. I did this twice (for a total of three). When I click on any of the objects all of them move.

There have been similar questions asked but none of them were solutions for me.

Thank you all in advance for any help you can provide.

Do you have any code which you are using in the script? You probably aren’t checking if the mouse is inside the GameObject.

So if your only checking if the mouse button is down, it will resolve to true and all the GameObjects will do the moving.

public class TestClass : MonoBehaviour
{
    private bool isMouseInside = false;

    public void OnMouseEnter()
    {
        isMouseInside = true;
    }

    public void OnMouseExit()
    {
        isMouseInside = false;
    }

    public void Update()
    {
        if (isMouseInside && Input.GetMouseButtonDown(0))
        {
            //Now the mouse is down, and inside your GameObject
        }
    }
}

That seems so obvious!
I suppose I might ought to have considered that a possibility.

I’ll give that a try. Thank you very much!