0
\$\begingroup\$

My trees keep on falling at the same time and I am not sure why! I am new to Unity3D javascript.

Here is my script:

#pragma strict

//INTS
var health = 5;
//GAMEOBJECTS
var tree : GameObject;
var log : GameObject;

function Start () {

tree = this.gameObject;

}

function Update () {

if(Input.GetKeyDown(KeyCode.Mouse0) && Player_script.axehave && health >= 1 && Camera_script.tree){
health -= 1;
}

if(health == 0){
tree.GetComponent.<Animation>().Play();
Destroy();
}

}

function Destroy(){

yield WaitForSeconds(2);
Destroy(tree.gameObject);

}

\$\endgroup\$
4
  • 2
    \$\begingroup\$ Maybe you intended something like Camera_script.tree == tree? Otherwise you just check that the camera script has a tree, any tree. \$\endgroup\$ Commented May 8, 2016 at 21:42
  • \$\begingroup\$ everything is working but the trees all lose health when ever i hit one tree and i was trying to fix it but i can't really find a way to do it! \$\endgroup\$
    – Aady Ahmed
    Commented May 8, 2016 at 22:19
  • \$\begingroup\$ sorry for my english \$\endgroup\$
    – Aady Ahmed
    Commented May 8, 2016 at 22:20
  • \$\begingroup\$ Ok, but what I am trying to say is that there is nothing saying you hit that specific tree in your code! Replace "&& Camera_script.tree" with "&& Camera_script.tree == tree" in your code and see what happens! \$\endgroup\$ Commented May 10, 2016 at 21:17

1 Answer 1

1
\$\begingroup\$

Nothing in this code snippet makes a distinction between this tree vs. the tree the player is looking at.

Unless that's what Camera_script.tree is supposed to do, but at long as that variable isn't null, the if-check will be true.

I think what you want there is Camera_script.tree == this.tree

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .