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 "Main"? 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 Main and current class (where OnTriggerEnter locates)

I'd love to help but, what @Denvery said...

In C#, I've seen this when there are missing { } - please scan for that.

I'm using JavaScript and all the brackets are all there.

2 Answers

2

Please try to add “public” to your “finish” definition? public static var finish : boolean = false;

Thank you so much- [1]- [2]- [3]- [4] [1]: https://editusstudio.wordpress.com [2]: http://ตัดต่อภาพ.exteen.com [3]: https://www.facebook.com/editusstudio [4]: http://รับตัดต่อรูปภาพเนียนๆ.blogspot.com

Same results happened. Still doesn't like what I'm doing.

Sorry, is your Main is a class? So is it defined like this:public class Main extends MonoBehaviour ?

In UnityScript, variables are public by default unless noted as private, so it doesn't have any effect...

And it extends MonoBehaviour by default too.

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…

It looks like it was modified with C#.

Do you mean your code and that's what the error was or mine? 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 MonoBehaviour {}'. 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 MonoBehaviour 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.