- Home /
Confusing error message
OK, Ive been trying to get to grips with the whole accessing variables from another script on another object. (don't even get me started, 21+ hours now and I still don't get it...lol)
Anyway.. my latest issue is that I am getting this error message -
Assets/Scripts/SlideDoor.js(7,37): UCE0001: ';' expected. Insert a semicolon at the end.
However the line of code it refers to is -
var gotKey : GameObject.GetComponent(PlayerInventory).hasKey;
As you can see it already has a semicolon at the end. So why am I getting this error and why is it telling me to put a semicolon at position 37 (this would not be at the end?)
Help plz guys
I am not really sure but try adding () after has$$anonymous$$ey because i think it may be a function, not a variable.
try var got$$anonymous$$ey = GameObject.GetComponent(PlayerInventory).has$$anonymous$$ey;
Can you post the whole file, or at least the method this error resides in?
@Eugenius, try var got$$anonymous$$ey = GameObject.GetComponent(PlayerInventory).has$$anonymous$$ey;
Gave me this -
Assets/Scripts/SlideDoor.js(7,25): BCE0020: An instance of type 'UnityEngine.GameObject' is required to access non static member 'GetComponent'.
???
Answer by BiG · Jul 14, 2013 at 08:16 AM
You should do
var gotKey : boolean = GameObject.Find("name").GetComponent(PlayerInventory).hasKey;
, where [name] is the object having the PlayerInventory script assigned to it.
That is: var [variable_name] : [variable_type] = [variable_assignment];
(obviously, you wasn't following this synthax, with your line of code). And, regarding the error about the missing semicolon: you are right, the semicolon was already here, but the console cannot show you always the exact error. In poor words, it has a predefined set of messages, and it shows the most suitable one for the actual situation. That's the reason for the "missing semicolon" message.
noob question but does ("name") in my case it would be Player go in quotation marks ("Player") or just brackets (Player)?
when just brackets I get -
Assets/Scripts/SlideDoor.js(7,43): BCE0005: $$anonymous$$ identifier: 'Player'.
when in quotes I get -
Assets/Scripts/SlideDoor.js(7,83): BCE0022: Cannot convert 'boolean' to 'UnityEngine.GameObject'.
???
You are absolutely right, my bad in defining got$$anonymous$$et as a GameObject, when it's obviously a boolean var. I updated my answer. And yes, use quotation marks, around Player.
now getting -
UnityException: You are not allowed to call this function when declaring a variable.
$$anonymous$$ove it to the line after without a variable declaration.
If you are using C# don't use this function in the constructor or field initializers, Ins$$anonymous$$d move initialization to the Awake or Start function.
SlideDoor..ctor () (at Assets/Scripts/SlideDoor.js:7)
So, that's an exposed variable (you are declaring it at the top of the script). Do just
var got$$anonymous$$ey : boolean;
And then use
got$$anonymous$$ey = GameObject.Find("name").GetComponent(PlayerInventory).has$$anonymous$$ey;
When you need it.
O$$anonymous$$ I put it in the awake event and did nothing, then I read your comment again and put it into the OnTriggerEnter event and O$$anonymous$$G YAHOO it works ! ! You, my friend are a legend. Finally (after almost 24 hours I can get some sleep...lol) And what even better is I think I now understand why its working.
$$anonymous$$any, many thanks (also to all you other peeps that have helped)
:)
Your answer
![](https://koobas.hobune.stream/wayback/20220613110056im_/https://answers.unity.com/themes/thub/images/avi.jpg)