- Home /
Scripting Errors I cannot fix, Could use a helping hand and someone to tell me how to fix it:PART II
Well for an assignment I was to fix a few scripts on my own. I managed to fix one fully by myself and another to only having a few errors which kind people on here helped me fix. Now Ive managed to get this final script down to the last few errors. (Going from 22 to 5 and being stuck) IF anyone is willing to assist me in fixing the errors and telling me what was wrong with them and how to fix it I would really be grateful. I'm very new to the scripting scene but I want to learn all I can!
public var health : int = 5; // number of hits before game over
public var moveSpeed : int = 3; // horizontal speed
public var jumpSpeed : float = 8; // jumping speed
public var turnSpeed : float = 2; // turning rate
private var controller : CharacterController; // reference to CharacterController component
private var velocity : Vector; // current speed
function Start ()
{
}
function Update ()
{
// assume no input
velocity.x = 0;
velocity.z = 0;
// forward
if (Input.GetKey("w") || Input.GetKey("up"))
{
velocity + transform.forward * moveSpeed;
}
// back
else if(Input.GetKey("s") || Input.GetKey("down"))
{
velocity + transform.back * moveSpeed;
}
// turn left
if(Input.GetKey("a") || Input.GetKey("left"))
{
transform.rotate(0, -turnSpeed, 0);
}
// turn right
else if(Input.GetKey("d") || ("right"))
{
Transform.Rotate(0, turnspeed, 0);
}
if(isGrounded)
{
// jump
if(Input.GetKeyDown("space"))
{
velocity += transform.up * jumpSpeed;
}
else
{
velocity.y = 0;
}
}
// gravity
velocity += Physics.gravity * Time.deltaTime;
// apply motion
controller.Move(velocity * Time.deltaTime);
function onTriggerEnter()
{
// detection radius
if(trigger.name == "Enemy")
{
// set enemy to alert
// Enemy is the name of the enemy script
// alert is a variable on the Enemy script
trigger.GetComponent(Enemy).alert = true;
}
// getting hit
else if(trigger.name == "Body")
{
health--;
if(health <= 0)
{
transform.DetachChildren();
Destroy(gameObject);
}
}
}
function OnTriggerEnter(trigger : collider)
{
// leaving detection radius
if(trigger.name == "Enemy")
{
trigger.GetComponent(Enemy).alert = false;
}
}
Format your code : http://video.unity3d.com/video/7720450/tutorials-using-unity-answers
Then actually tell us the errors. Once the script is readable, and the errors are clearly stated, then you will probably get some help.
Thanks to Statement for formatting your code. Don't forget to thank him, and then still watch the video I linked.
line 23, missing an equals sign :
velocity += transform.forward * moveSpeed;
line 28, missing an equals sign :
velocity += transform.back * moveSpeed;
line 33, capitol R in Rotate
transform.Rotate(0, -turnSpeed, 0);
that's all I can see without your error messages. Fix these, and then check for any other compilation errors. Be sure to tell us the errors, and remember to format your code =]
Oh, and line 3, Vector3 :
private var velocity : Vector3;
And there is something wrong with you having 2 function onTriggerEnter()
the first one is spelled wrong (lowercase o ins$$anonymous$$d of uppercase O), and has no parameters :
function onTriggerEnter()
The second one is what you want, move all the trigger code to there :
function OnTriggerEnter(trigger : collider)
This is a real cut and paste job here. You really need to experiment with some simple projects, and learn the basics of coding :
Also, the second OnTriggerEnter shouldn't be deleted but changed to OnTriggerExit.
Answer by Nachtmahr · Apr 04, 2013 at 08:23 PM
CharacterController was never assigned http://docs.unity3d.com/Documentation/ScriptReference/Component.GetComponent.html you do Calculation but no assignment yourvar = yourDesiredValue; Syntax Errors, wrong function names and Parameters take a look at http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.html
like alucardj suggestet learn the basics and how to find and resolve Bugs. Use the Manual there full of Examples of Code usage.