Ok, I give up - please help: Unexpected Token

I have searched all over on Google and read up on the phrase “unexpected Token” but I still cant figure out what I am missing here. Following the Will Goldstone book and I seem to have the exact code thats in there, thanks in advance.

The exact error is: Assets/Scripts/TargetCollision.js(39,1): BCE0043: Unexpected token: .

(Line 39 is not even a line, its ablank line after all the code and has nothing on it)
#pragma strict

private var beenHit : boolean = false;
private var targetRoot : Animation;
var hitSound : AudioClip;
var resetSound : AudioClip;
var resetTime : float = 3.0;

function Start () {

targetRoot = transform.parent.transform.parent.animation;

}

function Update () {

}

function OnCollisionEnter (theObject : Collision)
{
	if(beenHit == false && theObject.gameObject.name == "coconut")
	{
	StartCoroutine("targetHit");
	}
}

function targetHit()
{
audio.PlayOneShot(hitSound);
targetRoot.Play("down");
beenHit=true;
yield new WaitForSeconds(resetTime);
audio.PlayOneShot(resetSound);
targetRoot.Play("up");
beenHit=false;
}

@scriptRequireComponent(AudioSource)

If you remove this line

@scriptRequireComponent(AudioSource)

It will compile

Edit: You can move it to the top of the script under #pragma strict.

Unexpected token most likely just means unexpected character, try deleting everything in that line of code; double select the line and hit delete then hit enter again.

I just deleted and added an Audio Source manually to the prefab for now. But it would be nice to know how that @script code actually works, I will have to find out more on that I guess.

It looks like you are missing a space after “@script”. If you add it you should see the desired behavior of the component type getting added if it doesn’t already exist on the game object.

http://unitybook.net/book-errata/

Page 264 / 269 – Javascript code missing a space

@scriptRequireComponent(AudioSource)

should read -

@script RequireComponent(AudioSource)

You need a space between script and Require i.e.

@script RequireComponent(AudioSource)

If done correctly, the RequireComponent(AudioSource) will now be in all blue.