Simple car-enter script
Again, i need help... i made myself a new script to enter and descend a car (in Unity 5), but it doesnt work... any idea where the problem is? It says that some semicolons are missing, but theyre all there.. Its hard... Any example or aid with that script would be REALLY welcome... here´s the script:
var Cardrive = script; //Driving script for the car
var Cardrive = Boolean;
function Start () {
Cardrive = false;
}
function Update () {
function onTriggerEnter() { //i have a trigger next to the car´s left front door
animation.play() ("Enter"); //Player "enters" the car and takes place in the driver seat
Cardrive = true; //enables the car controlling script
}
if Input.GetKeyDown() ("e") //descend the car if you press "e"
animation.play() ("Exit") ; //player exits the car
Cardrive = false; //unables the car-Script
}
Answer by Statement · Nov 09, 2015 at 06:52 PM
any idea where the problem is?
Where to begin?
var Cardrive = script; //Driving script for the car
var Cardrive = Boolean;
^ You have two variables of the same name. You're assigning them to values that doesn't exist. You're not defining the type of Cardrive correctly.
function Update () {
function onTriggerEnter() {
^ You're nesting functions. onTriggerEnter should be OnTriggerEnter.
animation.play() ("Enter");
^ animation
in Unity 5 is now a Component and not an Animation. You've mistyped Play as play. You're trying to pass an argument to a void type.
if Input.GetKeyDown() ("e")
^ You've forgotton to use parenthesis for the if-statement. You're trying to pass an argument to a boolean type.
animation.play() ("Exit") ;
^ Same deal as with the other animation.play issue.
if Input.GetKeyDown() ("e")
animation.play() ("Exit") ;
Cardrive = false;
^ Apart from the two first lines which are syntax errors I already discussed, it looks like you got a logic error. Cardrive will always be false since you haven't used a statment block.
Apart from that, some comments make no sense. (What is "descending a car"?)
var Cardrive : boolean;
// doorKey is used to exit the vehicle.
var doorKey = "e";
private var anim : Animation;
function Start () {
Cardrive = false;
anim = GetComponent.<Animation>();
}
function Update () {
if (Input.GetKeyDown(doorKey))
{
// Player exits the car, playing an animation
// and disabling the car controller.
anim.Play("Exit");
Cardrive = false;
}
}
// I have a trigger next to the car´s left front door
function OnTriggerEnter() {
// Player enters the car and takes place in the
// driver seat. Car controller is enabled.
anim.Play("Enter");
Cardrive = true;
}
Note that the code doesn't change any variable in any other script. If you want to set Cardrive of another script, called CarController perhaps, remove Cardrive variable and use a controller variable ins$$anonymous$$d.
var controller : CarController;
function Start() {
controller.canDrive = false;
}
Thanks for your reply, i´m new with scripting so its now wonder tha my scripts are a bit worse... i try my best but sometimes it simply doesnt work:( otherwise thanks for your help. Regards PLay4freedomDE
If you want to become a developer, just accept that it is particularly hard in the beginning. Judging from your code output, I'd recommend you try to understand two separate parts of putting together text to resemble a program.
The first part is learning what the language syntax is. Together with syntax, you should also learn to understand compiler error messages, so you better can understand what went wrong. These two are "core program$$anonymous$$g" knowledge for the language you are using (UnityScript at the moment).
The second part is learning what the API's or libraries are. This is the classes and functions that Unity have created for you to use, when accessing UnityEngine, for example. With poor understanding of syntax, you'll have problems understanding how to use the libraries.
Syntax could be simplified as the "rules" and "patterns" of text in your code, and is specific to the language you are writing in. It doesn't matter if you are using Unitys libraries or writing code from scratch.
For example if Input.Get$$anonymous$$eyDown() ("e")
has two syntax errors. For example, an if-statement should be followed by parenthesis: if (Input.Get$$anonymous$$eyDown() ("e"))
would be "an improvement", but it still won't compile because Input.Get$$anonymous$$eyDown() ("e")
is also a syntax error and should be written as Input.Get$$anonymous$$eyDown("e")
. That is correct syntax for calling a function. To put them together: if (Input.Get$$anonymous$$eyDown("e"))
.
Unfortunately I don't know of a good resource of learning UnityScript syntax (also called "JavaScript", but it's confusing because there is another language called JavaScript that is not UnityScript). But the learn scripting section has a video of syntax differences with C#.
There are snippets you can find here and there. If you find a good resource on UnityScript syntax, let me know.
Thanks for your reply, Where can i learn sth like that (unity scripting basics) ? it would be nice if there would be a youtube tutorial for the basics of scripting... Thank you
The APIs or libraries you are using can (simply) be described as "the set of classes and functions that you have at your disposal", and you can write your own APIs.
For example animation.play();
would be correct syntax, but there are two problems here as well regarding your expectations of the API. In this case there isn't any play function on animation. The property animation has been deprecated in Unity 5 (quite recently), meaning it should no longer be used. This means Unity purposefully "cancelled" that property because it is being phased out. It doesn't mean you can't use animations, but that particular identifier can't be used. Unity changed the type of the identifier to Component, which does not have a play function. But even if we did have an identifier of type Animation (defined in Unitys API), there still doesn't exist a function called play. There is one called Play, however.
You can find the entire documented API in the scripting reference, and should be where you go when you want information about how to use the API.
Your answer
Follow this Question
Related Questions
Web GL Export Does Not Work in 2020.1.06b16 0 Answers
IL2CPP Windows Build 0 Answers
Failed to load mono 0 Answers
WebGL Error - Max Allowed WebGL Game .zip size is 500.00 MB 1 Answer