I am writing a script that would allow for the swapping of two other scripts. I keep getting errors. Does anyone know why?
Assets/Scripts/Switch.js(5,1): BCE0038: ‘void’ is not a valid macro.
var Switch : GameObject;
void Update () {
function if(Input.GetKeyDown("z"));
Switch.GetComponent(PlayerController).Enabled = true;
Switch.GetComponent(PlayerController1).Enabled = false;
}
- For javascript files, it is always a good idea to have #pragma strict at the top of the file.
- The way you are using ‘void’ here is how it is done in C#. In Javascript, you want to replace the ‘void’ with ‘function’.
- The ‘;’ at the end of the if() clause terminates the clause. This is not what you want.
- If your ‘if’ statement has multiple lines, you need them in brackets.
Putting it all together:
#pragma strict
var Switch : GameObject;
function Update () {
if(Input.GetKeyDown("z")) {
Switch.GetComponent(PlayerController).Enabled = true;
Switch.GetComponent(PlayerController1).Enabled = false;
}
}