GetComponent Script and Some basic JavaScript help

I got 2 scripts.


var cratePrefab : Transform;
var cratePocet = 0;

function Update () {

if (Input.GetButtonDown("Fire2") && cratePocet >= 1)
        var crate = Instantiate(cratePrefab, GameObject.Find("cratespawn").transform.position,
        print("Crate spawned!");


and addCrate.js

var setObject : GameObject.Find("Character (Lerpz)");
var script : SpawnCrate;

function OnTriggerEnter ( other : Collider ) {

setObject.script = Component.GetComponent("SpawnCrate");


This is my idea: On start, my character's cratePocet will be set to 0. If he triggers the trigger on object called addCratespin, it will + 1 to cratePocet of SpawnCrate.js

SpawnCrate.js is attached on character, addCrate.js is on addCratespin object. Iam getting this error:

Assets/Scripts/Objects/addCrate.js(2,32): UCE0001: ';' expected. Insert a semicolon at the end.


  1. `GameObject.Find("Character (Lerpz)");` is not a type. Perhaps you meant `var setObject : GameObject = GameObject.Find("Character (Lerpz)");` ?
  2. You shouldn't(can't?) call GameObject.Find in an object initialization. You would need to put it in Start.
  3. You shouldn't use GameObject.Find at all if you can avoid it. It is terribly expensive. In this case, you might consider simply setting the reference in the editor. If you must use a Find, try GameObject.FindWithTag in stead.
  4. setObject has a variable called .script? GameObjects don't have any such variable. What type is it supposed to be?
  5. Is there an instance of SpawnCrate attached to this GameObject? If not, then why are you treating it like it is?

A corrected script would look like:

var setObject : GameObject;
var script : SpawnCrate;

function Start() {
    //setObject = GameObject.FindWithTag("Player"); //If you must...
    //setObject = GameObject.Find("Character (Lerpz)"); //as a last resort
    script = setObject.GetComponent("SpawnCrate");

function OnTriggerEnter ( other : Collider ) {
    if(script) script.cratePocet++;

As a point though, an alternative approach is to not store the references in this script at all but to figure it out in OnTriggerEnter. Something like:

function OnTriggerEnter ( other : Collider ) {
    if(other.tag == "Player") {
    //if( == "Character (Lerpz)") { //Valid alternative
        var script : SpawnCrate = other.GetComponent("SpawnCrate");
        if(script) script.cratePocet++;

Now, it shows me this errors:

Assets/Scripts/Objects/addCrate.js(10,15): BCE0019: 'script' is not a member of 'UnityEngine.GameObject'. 

Assets/Scripts/Objects/addCrate.js(10,34): BCE0020: An instance of type 'UnityEngine.Component' is required to access non static member 'GetComponent'.

Assets/Scripts/Objects/addCrate.js(11,15): BCE0019: 'script' is not a member of 'UnityEngine.GameObject'. 

Assets/Scripts/Objects/addCrate.js(11,22): BCE0049: Expression cannot be assigned to.

As said up, i have attached addCrate to addCratespin - a cube, wich rotates and SpawnCrate to Character (Lerpz) - that are names in hierarchy.

I have deleted the //If you must (only //) and /* + */