- Home /
Scripting Errors I cannot fix, Could use a helping hand and someone to tell me how to fix it.
Hello everyone, just getting started with Unity and Javascript and have started debugging a script my teacher provided us, I have managed to do most of it but have one error popping up here. If anyone is willing to help me fix the errors and describing what is wrong with and and how to fix it I would be super thankful. Thanks alot
public var speed : float = 2; // movement speed
public var patrolDistance : float = 10; // how far to patrol
public var alert : Bool = false; // is alert to player's presence?
private var player : Transform; // reference to the player's transform
private var patrolledSoFar : float = 0; // current progress through patrol
function Start()
{
// get reference to CC
controller = GetComponent(CharacterController);
// find player using tag
player = GameObject.FindWithTag("Player").transform;
}
function Update()
{
// if the player has been spotted...
if(alert)
{
// search and destroy
transform.LookAt(player);
else // if the player is undetected //first error Here says "expecting }, found "else"
{
// patrol
patrolledSoFar == speed * Time.deltaTime; //Second error says "expecting :, found ";"
if(patrolledSoFar == patrolDistance)
{
transform.Rotate(0, 180, 0);
}
}
// always move
controller.Move(transform.forward * speed * Time.deltaTime);
}
As you can see people are happy to help! be sure to TIC$$anonymous$$ an answer when you are finished, to close out the question. Also THU$$anonymous$$B any useful answers!
Thanks alot for the help, I always feel kinda lame when I have to ask someone to point out something easy in Java, but I really am grateful for your help, and thank you for labeling the errors.
Answer by xolan · Apr 04, 2013 at 10:49 AM
First thing in Java there isnt a variable type called "Bool" instead you should use "boolean".
so the corect way to write the alert variable would be:
var alert:boolean = false;
Yea and I found a couple of syntax errors. The script should be somthing like this.
var speed:float = 2;
var patrollDistance:float = 10;
var alert:boolean = false;
private var player:Transform;
var patrolledSoFar:float = 0;
function Start()
{
controller = GetComponent(CharacterController)
player = gameObject.FindWithTag("Player").transform;
}
function Update()
{
//See if the player has been spotted
if(alert == true)
{
transform.LookAt(player);
} //This is the first error. You havent placed the closed bracket so it says(Expecting "}" found else)
else
{
patrolledSoFar = speed * Time.deltaTime; //Here only goes one "="
if(patrolledSoFar == patrollDistance)
{
transform.Rotate(0,180,0);
}
}
//Making it always moving can be done like this
transform.position += transform.forward * speed //This is only if your method doesent work if it does leave it as it is.
}
Thats about all the scripting you need as far as I could read the script and what you wanted to do with it.
I hope I helped
Answer by Chronos-L · Apr 04, 2013 at 10:41 AM
#pragma strict
public var speed : float = 2;
public var patrolDistance : float = 10;
public var alert : boolean = false;
private var player : Transform;
private var patrolledSoFar : float = 0;
// You need this line or else
// you will get BCE0005: Unknown identifier: 'controller'
private var controller : CharacterController;
function Start()
{
controller = GetComponent(CharacterController);
player = GameObject.FindWithTag("Player").transform;
}
function Update()
{
if(alert)
{
transform.LookAt(player);
} // <- the missing "}", You need this to close the if block.
// Fixing this also fix the "expecting :, found ";" down there
else
{
//I got a "BCE0034: Expressions in statements must
//only be executed for their side-effects." after I
//fix both error by adding a } to close your if block.
//BCE0034 is caused by the '==', it should be '='
patrolledSoFar = speed * Time.deltaTime;
if(patrolledSoFar == patrolDistance)
{
transform.Rotate(0, 180, 0);
}
}
controller.Move(transform.forward * speed * Time.deltaTime);
}
Your answer
