I want to be able to tap a switch and have it slide to the other side. Right now that switch is a prefab with my code attached. When I add copies of that prefab all of them move not the individual one I clicked.
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100.0f))
{
PrintName(hit.transform.gameObject);
}
}
}
private void PrintName(GameObject go)
{
print(go.name);
transform.position = new Vector3(3, 0, 0);
}
}
CodePudding user response:
You are probably referencing the original prefab asset instead of the just the object you want to move, and so you are making changes to everything.
CodePudding user response:
update function runs for all of your prefabs. basically, when you hit something with your raycast to something, every prefab that the script attached run PrintName function. thats why all of the prefabs move simultaneously.
ps: maybe you should go.transform.position instead of transform.position