Ok, so i am working on a wood cutting kind of game and so far i am able to cut a tree with a chainsaw and create a log. But when i copy my first tree in the hierarchy and paste it to make another one. It does not let me cut the second tree until i have cut the first tree.
for example, if i paste a copy of a tree in the scene, i have to cut the first tree before i can cut the second. If i try to cut the other tree first it will not let me
my code for cutting trees:
var bullitPrefab:Transform;
function Start () {
}
function Update () {
if(Input.GetButtonDown("placeLog")) {
if(Vector3.Distance(GameObject.Find("BigTree").transform.position, transform.position) < 5){
Destroy(GameObject.Find("BigTree"));
var bullit = Instantiate(bullitPrefab, GameObject.Find("logSpawn").transform.position, Quaternion.identity);
}
}
}
“BigTree” is the name of the tree in the hierarchy.
Its because you are using Find to decide which tree to cut.
Of course if there are multiple 'BigTree’s its only going to return the first one in your hierarchy.
Here is a better way to do it…
var trees: GameObject[];
function Start() {
trees = GameObject.FindGameObjectsWithTag("Tree");
}
function Update()
{
if(Input.GetButton("placeLog")) {
foreach(var tree in trees) {
if(tree == null)
continue;
if(Vector3.Distance(tree.transform.position, transform.position) < 5)
{
Instantiate(bullitPrefab, tree.transform.position, Quaternion.Identity);
Destroy(tree);
break;
}
}
}
}
Make sure you setup your trees with a Tree tag
Ok, now im gettings these:
Assets/Standard Assets/Terrain Assets/Trees Ambient-Occlusion/Palm/cutTree.js(18,11): UCE0001: ‘;’ expected. Insert a semicolon at the end.
Assets/Standard Assets/Terrain Assets/Trees Ambient-Occlusion/Palm/cutTree.js(18,12): BCE0043: Unexpected token: var.
Assets/Standard Assets/Terrain Assets/Trees Ambient-Occlusion/Palm/cutTree.js(18,16): BCE0044: expecting ), found ‘trees’.
Assets/Standard Assets/Terrain Assets/Trees Ambient-Occlusion/Palm/cutTree.js(18,30): BCE0043: Unexpected token: ).