- Home /
Referencing external static variable with no success
This does not make any sense whatsoever. I followed the rules, I referred to my previous successful works, I checked if the variables were static (and they are), I checked the API, I've done everything and this code still says "'finish' is not a member of 'function(): void'".
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Finish"){
Debug.Log("Finish");
Main.finish = true;
}
}
The variable it is referencing is also listed below: static var finish : boolean = false;
You have a class named "$$anonymous$$ain"? Could be a reserved word.
EDIT: Looks like reserved words are few and far between. http://forum.unity3d.com/threads/a-question-about-special-or-reserved-variable-names.94421/
Please post your code for classes $$anonymous$$ain and current class (where OnTriggerEnter locates)
I have another code calling on one of $$anonymous$$ain's static variables and that one has no issues at all, so it isn't a reserved word.
Here's the code for $$anonymous$$ain:
static var finish : boolean = false;
static var dead : boolean = false;
static var triggered : boolean = false;
private var musicSwapped : boolean = false;
var unseen$$anonymous$$usic : AudioClip;
var seen$$anonymous$$usic : AudioClip;
var colliding : Collider;
var BG$$anonymous$$ : AudioSource;
function Start(){
BG$$anonymous$$ = GetComponent.<AudioSource>();
Cursor.visible = false;
Cursor.lockState = CursorLock$$anonymous$$ode.Locked;
BG$$anonymous$$.clip==unseen$$anonymous$$usic;
BG$$anonymous$$.Play();
}
function Update(){
if (triggered == true && musicSwapped == false){
BG$$anonymous$$.volume -= 1 * Time.deltaTime;
if (BG$$anonymous$$.volume <= 0.01){
musicSwapped = true;
BG$$anonymous$$.clip = seen$$anonymous$$usic;
BG$$anonymous$$.volume = 1;
BG$$anonymous$$.Play();
}
}
if(Input.GetButtonDown("Cancel")){
Application.Quit();
}
if(finish){
Finished();
}
if(dead){
Deaded();
}
}
function Finished(){
Debug.Log("Finished");
if (triggered) {
Application.LoadLevel("GameOverSeen");
} else {
Application.LoadLevel("Victory");
}
}
function Deaded(){
Debug.Log("Dead");
if (triggered) {
Application.LoadLevel("GameOverSeen");
} else {
Application.LoadLevel("GameOver");
}
}
Code for OnTriggerEnter was listed but I messed it up so it looked like unreadable gibberish. I've fixed the appearance so you can actually read it.
In C#, I've seen this when there are missing { } - please scan for that.
Answer by ransomink · Aug 20, 2015 at 04:00 AM
I copied your code, placing the main script on a gameobject and the trigger script on a different gameobject. I indeed received the same error as you: "'finish' is not a member of 'function(): void'". I fixed the problem by placing both, the 'Main' script and the script name used by the OnTriggerEnter inside of a class definition. Details below...
Main script:
#pragma strict
public class Main extends MonoBehaviour
{
static var finish : boolean = false;
static var dead : boolean = false;
static var triggered : boolean = false;
var unseenMusic : AudioClip;
var seenMusic : AudioClip;
var colliding : Collider;
var BGM : AudioSource;
private var musicSwapped : boolean = false;
function Start(){
BGM = GetComponent.<AudioSource>();
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
BGM.clip==unseenMusic;
BGM.Play();
}
function Update(){
if (triggered == true && musicSwapped == false){
BGM.volume -= 1 * Time.deltaTime;
if (BGM.volume <= 0.01){
musicSwapped = true;
BGM.clip = seenMusic;
BGM.volume = 1;
BGM.Play();
}
}
if(Input.GetButtonDown("Cancel")){
Application.Quit();
}
if(finish){
Finished();
}
if(dead){
Deaded();
}
}
function Finished(){
Debug.Log("Finished");
if (triggered) {
Application.LoadLevel("GameOverSeen");
} else {
Application.LoadLevel("Victory");
}
}
function Deaded(){
Debug.Log("Dead");
if (triggered) {
Application.LoadLevel("GameOverSeen");
} else {
Application.LoadLevel("GameOver");
}
}
}
Name of the script and class that uses the OnTriggerEnter (I called mine Trigger for testing purposes):
#pragma strict
public class Trigger extends MonoBehaviour
{
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Finish"){
Debug.Log("Finish");
Main.finish = true;
}
}
}
I tried it myself and it worked out fine, so check it and let me know that it works...
Do you mean your code and that's what the error was or $$anonymous$$e? If you're referring to my code, it does not use C#, I only know and use UnityScript.
I copied and pasted your code into my visual editor. What I did was encapsulate the code inside of a class definition using 'public class "ClassName" extends $$anonymous$$onoBehaviour {}'.
Besides that I reorganized the order of your variables placing first: static, public, then private. (Just how I do my work, that's all.)
I think I see your confusion: C# scripts must have a class definition with the same class name as the script; UnityScript extends $$anonymous$$onoBehaviour by default so there is no need for a class definition. Certain circumstances you need the class definition, i.e. inheritance and interfacing.
I added the class definition to check if it worked and it did...
I've finally gotten around to plugging in the code and it works without a problem. Don't know how it works, but I'll look into that later on.
Answer by Denvery · Aug 19, 2015 at 09:54 AM
Please try to add "public" to your "finish" definition? public static var finish : boolean = false;
Sorry, is your $$anonymous$$ain is a class? So is it defined like this:public class $$anonymous$$ain extends $$anonymous$$onoBehaviour ?
In UnityScript, variables are public by default unless noted as private, so it doesn't have any effect...