Tree Chopping

So in my game, i programed so the first person (me) whould chop down a tree, then it would fall and coconuts and wood would spawn next to it. but i have 3 errors that i could not find how to fix. ( i even did some research).

Here are the errors:

Assets/Part 4 - Tree Chopping/TreeController.js(14,49): BCE0043: Unexpected token: ).

Assets/Part 4 - Tree Chopping/TreeController.js(14,50): BCE0044: expecting ), found ‘.’.

Assets/Part 4 - Tree Chopping/TreeController.js(14,51): UCE0001: ‘;’ expected. Insert a semicolon at the end.

Here is the code:


#pragma strict

var treeHealth : int = 5;

var logs : Transform;
var coconut : Transform;
var tree : GameObject;

var speed : int = 8;

function Start()
	tree = this.gameObject;
	this.gameObject.GetComponent<Rigidbody>().isKinematic = true;


function Update()
	if(treeHealth <= 0)
		rigidbody.isKinematic = false;
		rigidbody.AddForce(transform.forward * speed);

function DestroyTree()
	yield WaitForSeconds(7);
	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(2,2,0) + position, Quaternion.identity);
	Instantiate(coconut, tree.transform.position + Vector3(5,5,0) + position, Quaternion.identity);

Is there something i did wrong?

Your main problem is cargo-cult programming. There are actually quite a few errors in your code, but I’ll answer your question:

You’ve copy-pasted code from the wrong language. JS doesn’t use the same generics syntax as C#. Unity has provided some helper methods though, so you can fix the immediate error quite easily by inserting a dot in front of your opening angle brace:

gameObject.GetComponent.<Rigidbody>().isKinematic = true;

Your Destroy(tree) function is running before you spawn the objects that depend on the tree’s location.

Tip: Spawn(instantiate) the objects first, then Destroy(tree).

Why? It happens in the same frame so the order doesn’t make a difference to the human eye.