Please tell me what is wrong with this Projectile.js file

Here is my code for Projectile.js on my FPS unity project

Code:

function Start () {

}

function Update () {
    var explosion : GameObject;
    function OnCollisionEnter( collision : Collision )
    {
        var contact : ContactPoint = collision.contacts[0];
        var rotation = Quaternion.FromToRotation( Vector3.up, contact.normal );
        var instantiatedExplosion : GameObject = Instantiate(
            explosion, contact.point, rotation );
        Destroy( gameObject );
    };
}

Apparently, I’m running into problems with it.

First off, if you are having problems you should describe them instead of just wholesale posting your script and expecting people to analyse it for you.

Secondly, in this case your problems are thankfully fairly obvious.

You should see if you can find some basic coding tutorials. It’s fairly obvious from what you’ve posted that you’re copying things out of other people’s code / tutorials without any real understanding of what functions are or how they interact, or what syntax you need to put in where.

In this case, the problem is your usage of OnCollisionEnter. function OnCollisionEnter(collision : Collision) declares the beginning of a function (in this case, an automatic Unity callback that gets used whenever your object collides with another object). This means it should be inside your class, but outside any other functions! What you’ve done here, is stick it right in the middle of your Update function!

You need to seperate them out into different sections. A function is declared like so-

function [nameOfFunction] (/*any number of arguments here*/) : [optional return type]
{
    // functionality goes here. Anything you want the function to do
    // needs to go between the curly braces at the top and bottom.

    // keep in mind that you *may not* (in JS at least) declare another
    // function inside this function.

    [if you defined a return type, you also need to include a return line]
    [return *someObjectOfTheCorrectType*]

} // There isn't a semicolon here, because it's a function, not a statement.

Also, you should make sure that you always keep it tabbed properly. This isn’t so much so that the computer can read what you’re doing, so much as it is so that you can understand your own code! It’s very hard to read things that just look like a square block of text.