Okay so i made a script that uses ray casting to tell if you clicked on a tree and if you click on a tree it falls, but the problem is i am using a prefab of a tree with this script on it, so when i click on 1 tree they all fall because they are all using the same script and are all just instances of the prefab, i was wondering what would be a solution to this.
Here is the script im using:
using UnityEngine;
using System.Collections;
public class treeFalling : MonoBehaviour {
//Get the rigidbody
public Rigidbody rb;
//Get the raycast
public Ray ray;
public RaycastHit hit;
// Use this for initialization
void Start() {
//Get components
rb = GetComponent<Rigidbody>();
//Tree rotation
treeRotation();
}
// Update is called once per frame
void Update() {
treeRaycast();
}
public void treeRaycast() //Check if the player clicked the tree and if they did make it fall
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(ray, out hit, 100))
{
if (hit.transform.tag == "chopable")
{
rb.isKinematic = false;
}
}
}
}