all objects play animation at the same time when clicking one of them

Hello guys! I’m new around here and to the world of programming but I’m trying to learn all I can about making games with Unity! I Got plenty of help from this community but I can’t seem to figure this problem out.

I have a smple script that shakes a bush when I click on it. The problem is when there are multiple bushes, clicking on any of them will shake them all at once.

I would like to only have the bush that has been clicked to be shaking,

Here is my code:

using UnityEngine;
using System.Collections;

public class bushShake : MonoBehaviour {
	void Update () {

		if(Input.GetMouseButtonDown(0))
		{
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			RaycastHit hit;

			if(Physics.Raycast(ray, out hit, 7))
			{				
				Animator anim = GetComponent<Animator>();
				anim.Play("anim_p_bush_shake");
				Debug.Log ("hit: " + hit.distance);
			}
		}
	}
}

Anybody has an idea on how I could fix it?

Many thanks!

Julien

Hi Julien @Complexium . this is because you are running the code in update. all the bushes run the same code and they all start animating. and you are not specifying which bush to animate.
i will give you simple code to replace with it.

use this

void OnMouseDown()
    {
        Animator anim = GetComponent<Animator>();
        anim.Play("anim_p_bush_shake");
    }

This will only run the code and animation for the bush you click/tap on

Hope this helps

Worked just fine! MANY thanks @rrabi !

To understand correctly: Why do we still need a collider on the bushes even tho we are not using the raycasting code?

void OnMouseDown() always requires a collider to be effective?