- Home /
Dreaded UCE0001 ';' error
Hello UnityAnswers,
As a generally new programmer I am still stumbling upon sites to help me with my school work and get useful tips,tricks, and incite to sharpen my skills but it's hard when you drop face first into an error you cant seem to fix.
`using UnityEngine;
using System.Collections;
var gun1 = GameObject;
gun1 = GameObject.Find("Box02");
var gun2 = GameObject;
gun2 = GameObject.Find("ak47");
function Start() {
}
function Update() {
if (Input.GetButtonDown('1')) {
gun2.SetActiveRecursively(false);
gun1.SetActiveRecursively(true);
}
}`
a reeeally simple weapon switching script will come from what I have so far but not if I cant get passed "(1,6): UCE0001: ';' expected. Insert a semicolon at the end." I says I do not have a semi-Colon at the end of my first line, but like so many scripts before me it clearly does. Reading through other forum posts from many different sites I have seen that it could be a formatting or syntax error, only a few weeks into unity itself I am not FULLY adjusted to its syntax and could be missing something very obvious. Any help would be Bueno!
Thanks for your time.
Thank you for the detailed reply! hmmm alright, I switched things around a bit but am still getitng the same error on the same line. The two items that are getting found are childes of the Firstpersoncontroler>$$anonymous$$aincamera>Weapons>Box02,ak47. I dont know if that would cause problems or if I called it wrong because of that fact?
using UnityEngine;
using System.Collections;
var gun1 : GameObject;
var gun2 : GameObject;
function Start()
{
gun1 = GameObject.Find("Box02");
gun2 = GameObject.Find("ak47");
}
function Update() {
if (Input.GetButtonDown('1')) {
gun2.SetActiveRecursively(false);
gun1.SetActiveRecursively(true);
}
}
I know it's tempting because the buttons are labeled so poorly, but please don't post updates to your question as an answer...
(btw, it doesn't give you the line for no reason at all; though the problem is often not exactly at the given point, it does give a hint as to what might be wrong. So it usually makes sense to give the information here.)
No, hirarchy-quirks can't lead to syntax-errors, but only to runtime-errors.
Hint: Not sure how much actual value this list has, but maybe you can get something out of it...? http://answers.unity3d.com/questions/155200/stuff-is-going-wacky-checklist-compiler-errors-syn.html
Ahhh Thank you for all your help!
I will continue to work through this and figure out the rest! Thank you again.
Answer by SisterKy · Feb 01, 2012 at 08:50 PM
I'll assume the ' at the start and end are only here, not in your actual script... (you'd have to remove them)
Anyway, here's what I see:
var gun1 = GameObject;
I think you don't want to put a non-existent thing called "GameObject" inside your new variable; instead, you want to set the type; what kind of variable this is going to be. You do that with a colon instead of a equal-sign
var gun1 : GameObject;
also, you need to do the
gun1 = GameObject.Find("Box02");
inside of a function.
If you create a new value (like a number or a string) it's completely legal to do it the way you did.
But GameObjects work a bit different, so as a rule of thumb: If you assign a GameObject - whether you instantiate it in the script or take it from the scene (as you do here) - do it inside a function.
Preferably inside
function Start () {}
You could also consider
function Awake () {}
provided, that "Box02" is already in the scene when the game starts.
Function Awake gets called before anything else at the very beginning of the game and should be used to set things up. But if you might later choose to instantiate "Box02" at runtime by a different script - even if you do it in function Awake also, this other script might get called after this script and the Find would turn out empty and you'd get a nullreferenceexception... so probably better do it in Start...
Well, that's what I see at first glance... I'm not sure, however, if that will fix your problem...
Answer by SisterKy · Feb 05, 2012 at 11:48 AM
I think I have an idea what's causing the problem. Sometimes I don't see the wood because of all the trees.....
What language are you using? the 'using' commands are C# and I think they are illegal in .js; the syntax for variable-declaration is javascript. So check the filename-extension and adjust accordingly.
If it's C# you need to wrap everything except for 'using' inside
public class NameOfYourFile : MonoBehaviour {}
and change variable-declaration-syntax to
private GameObject gun1;
'private' is the right here, because you 'Find' the ObjectReference; If you wanted to drag-and-drop the reference in the Editor instead, it would need to be 'public' (if I'm not misstaken)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
attract particles 3 Answers
Every 0.5 sec there is a 10% chance of a target spawning. 2 Answers
How can I make a function wait before it starts running again? 1 Answer
help unity3d javascript 1 Answer