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!
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…
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)