I have made a script for chopping down a tree and spawning items when its destryed. it isn't working and I don't know why:
#pragma strict
var treeHealth : int = 5; //tree health starts at 5
var logs: Transform;
var coconut : Transform;
var tree : GameObject;
var speed : int =8;
function Start()
{
tree = this.gameObject; // name tree as gameobject
GetComponent.().isKinematic = true; // Ensure that it is set as kinematic
}
function Update()
//if the tree his hit minus 1 health
//psydocode:
//if object tag tree is hit by object with tag axe, -1 health
{
if(treeHealth <=0) // if the tree has no health
{
GetComponent.().isKinematic = false; //take off kinematic
GetComponent.().AddForce(transform.forward * speed); //make the tree fall
DestroyTree();
}
}
function DestroyTree() //call the destroyTree func
{
yield WaitForSeconds(7); //make it fall for 7 seconds
Destroy(tree); //destroy it
//spawn logs and coconuts
var position : Vector3 = Vector3(Random.Range(-1.0, 1.0), 0, Random.Range(-1.0, 1.0));
Instantiate(logs, tree.transform.position + Vector3(0,0,0) + position, Quaternion.identity);
Instantiate(logs, tree.transform.position + Vector3(2,2,0) + position, Quaternion.identity);
Instantiate(logs, tree.transform.position + Vector3(5,5,0) + position, Quaternion.identity);
Instantiate(coconut, tree.transform.position + Vector3(0,0,0) + position, Quaternion.identity);
Instantiate(coconut, tree.transform.position + Vector3(4,4,0) + position, Quaternion.identity);
Instantiate(coconut, tree.transform.position + Vector3(3,3,0) + position, Quaternion.identity);
}
↧