- Home /
Trouble with switching script
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;
}
Answer by robertbu · May 17, 2014 at 01:02 AM
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;
}
}
Thanks! Now i'm getting the errors: Assets/Scripts/Switch.js(9,25): BCE0005: $$anonymous$$ identifier: 'PlayerController1'. Assets/Scripts/Switch.js(8,25): BCE0005: $$anonymous$$ identifier: 'PlayerController'.
Any idea why?
$$anonymous$$ identifier
There is no class or component in your project with those names.
i.e : you don't have any scripts named PlayerController or PlayerController1 in your project.
also, I'm fairly sure enabled should be lowercase e (unless it is a public variable of the controller scripts).
Sorry. Only, there are Components named PlayerContoller.
Are they attached to the gameObject Switch ?
Also, are they in uJS or C#. It seems you are mixing languages (and/or mixed up on languages). If they are in C#, then uJS scripts cannot see them.
http://answers.unity3d.com/questions/385582/how-to-connect-2-scripts-using-c-js.html
Thank you! The other scripts were in C#. I'm still trying to make heads or tails of the link though. I know you are probably very busy, but is there a chance you could help me convert this script to C#?