Fps Scriting error.

I have an error with my scripting which will be at the bottom.Script:

/*
	Usage:

	// Load my level	
	LevelLoadFade.FadeAndLoadLevel("mylevel", Color.white, 0.5);

	// Reset the current level
	LevelLoadFade.FadeAndLoadLevel(Application.loadedLevel, Color.white, 0.5);
*/

static function FadeAndLoadLevel (level, fadeTexture : Texture2D, fadeLength : float)
{
	if (fadeTexture == null)
		FadeAndLoadLevel(level, Color.white, fadeLength);

	var fade = new GameObject ("Fade");
	fade.AddComponent(LevelLoadFade);
	fade.AddComponent(GUITexture);
	fade.transform.position = Vector3 (0.5, 0.5, 1000);
	fade.guiTexture.texture = fadeTexture;
	fade.GetComponent(LevelLoadFade).DoFade(level, fadeLength, false);
}

static function FadeAndLoadLevel (level, color : Color, fadeLength : float)
{
	var fadeTexture = new Texture2D (1, 1);
	fadeTexture.SetPixel(0, 0, color);
	fadeTexture.Apply();
	
	var fade = new GameObject ("Fade");
	fade.AddComponent(LevelLoadFade);
	fade.AddComponent(GUITexture);
	fade.transform.position = Vector3 (0.5, 0.5, 1000);
	fade.guiTexture.texture = fadeTexture;

	DontDestroyOnLoad(fadeTexture);
	fade.GetComponent(LevelLoadFade).DoFade(level, fadeLength, true);
}

function DoFade (level, fadeLength : float, destroyTexture : boolean)
{
	// Dont destroy the fade game object during level load
	DontDestroyOnLoad(gameObject);

	// Fadeout to start with
	guiTexture.color.a = 0;
	
	// Fade texture in
	var time = 0.0;
	while (time < fadeLength)
	{
		time += Time.deltaTime;
		guiTexture.color.a = Mathf.InverseLerp(0.0, fadeLength, time);
		yield;
	}
	guiTexture.color.a = 1;
	yield;

	// Complete the fade out (Load a level or reset player position)
	Application.LoadLevel(level);
	
	// Fade texture out
	time = 0.0;
	while (time < fadeLength)
	{
		time += Time.deltaTime;
		guiTexture.color.a = Mathf.InverseLerp(fadeLength, 0.0, time);
		yield;
	}
	guiTexture.color.a = 0;
	yield;

	Destroy (gameObject);

	// If we created the texture from code we used DontDestroyOnLoad,
	// which means we have to clean it up manually to avoid leaks
	if (destroyTexture)
		Destroy (guiTexture.texture);
}

error:

Assets/Misc Scripts/LevelLoadFade.js(60,30): BCE0004: Ambiguous reference 'LoadLevel': UnityEngine.Application.LoadLevel(String), UnityEngine.Application.LoadLevel(int).

1 Answer

1

You should really switch to using C#. With javascript you do not get as much compilation error help from the IDE. Some problems that would have been detected at compilation if done in C# are instead found at runtime, making it twice as hard to debug. The other problem with javascript is you don’t get strong type casting, which is exactly what you have here. The type of the variable “level” is not defined because javascript does not make you define them. At compilation, the unity compiler does not know if your “level” variable will be an int, a string, an object, etc. The function “LoadLevel” is “overloaded”. This means there are multiple functions in the Application class with that name, but different arguments. One takes a string as an argument, the other takes an int. Since the compiler does not know what type “level” is, it does not know which method to use when it compiles your application, so it cannot compile.

You need to tell the script what type your variable “level” is. Change this line:

function DoFade (level, fadeLength : float, destroyTexture : boolean)

~to~

function DoFade (level : int, fadeLength : float, destroyTexture : boolean)

~or~

function DoFade (level : String, fadeLength : float, destroyTexture : boolean)