- Home /
Problem with C#.
This script named "AutoFade"
using UnityEngine;
using System.Collections;
public class AutoFade : MonoBehaviour
{
private static AutoFade m_Instance = null;
private Material m_Material = null;
private string m_LevelName = "";
private int m_LevelIndex = 0;
private bool m_Fading = false;
private static AutoFade Instance
{
get
{
if (m_Instance == null)
{
m_Instance = (new GameObject("AutoFade")).AddComponent<AutoFade>();
}
return m_Instance;
}
}
public static bool Fading
{
get {
return Instance.m_Fading; }
}
private void Awake()
{
DontDestroyOnLoad(this);
m_Instance = this;
m_Material = new Material("Shader \"Plane/No zTest\" { SubShader { Pass { Blend SrcAlpha OneMinusSrcAlpha ZWrite Off Cull Off Fog { Mode Off } BindChannels { Bind \"Color\",color } } } }");
}
private void DrawQuad(Color aColor,float aAlpha)
{
aColor.a = aAlpha;
m_Material.SetPass(0);
GL.Color(aColor);
GL.PushMatrix();
GL.LoadOrtho();
GL.Begin(GL.QUADS);
GL.Vertex3(0, 0, -1);
GL.Vertex3(0, 1, -1);
GL.Vertex3(1, 1, -1);
GL.Vertex3(1, 0, -1);
GL.End();
GL.PopMatrix();
}
private IEnumerator Fade(float aFadeOutTime, float aFadeInTime, Color aColor)
{
float t = 0.0f;
while (t<1.0f)
{
yield return new WaitForEndOfFrame();
t = Mathf.Clamp01(t + Time.deltaTime / aFadeOutTime);
DrawQuad(aColor,t);
}
if (m_LevelName != "")
Application.LoadLevel(m_LevelName);
else
Application.LoadLevel(m_LevelIndex);
while (t>0.0f)
{
yield return new WaitForEndOfFrame();
t = Mathf.Clamp01(t - Time.deltaTime / aFadeInTime);
DrawQuad(aColor,t);
}
m_Fading = false;
}
private void StartFade(float aFadeOutTime, float aFadeInTime, Color aColor)
{
m_Fading = true;
StartCoroutine(Fade(aFadeOutTime, aFadeInTime, aColor));
}
public static void LoadLevel(string aLevelName,float aFadeOutTime, float aFadeInTime, Color aColor)
{
if (Fading) return;
Instance.m_LevelName = aLevelName;
Instance.StartFade(aFadeOutTime, aFadeInTime, aColor);
}
public static void LoadLevel(int aLevelIndex,float aFadeOutTime, float aFadeInTime, Color aColor)
{
if (Fading) return;
Instance.m_LevelName = "";
Instance.m_LevelIndex = aLevelIndex;
Instance.StartFade(aFadeOutTime, aFadeInTime, aColor);
}
}
This script is named "LoadLevel"
using UnityEngine;
using System.Collections;
public class LoadLevel : MonoBehaviour
{
void OnTriggerEnter(Collider other){
AutoFade.LoadLevel("Home" ,3,1,Color.black);
}
}
"I got an error "The class defined in script file named 'LoadLevel' does not match the file name!"
Is using 'LoadLevel' the wisest choice? It's a well known function already in use and yet you define a function LoadLevel, overload that function and define a class by the same name.
Also, check in the file explorer (windows one not unity) for duplicate files.
Duplicates don't normally show in project manager. If you dragged and dropped a file to replace an existing one the old one is renumbered, not overwritten (in my experience).
I suspect you have 2 LoadLevel.cs scripts at different locations in your Assets folder. One of them has the LoadLevel class and the other the FadeOut class, which gives the error.
There two mistakes you are making here, unrelated to your code. First you post an update as an answer, meaning now your question shows 3 answers. many people will just skip your questions thinking among the three, you probably have your answer.
Second you ask someone in particular, probably because you know that he knows much. Well, then why should I help you if you are waiting on someone else...
@fafase +1: Some forum members think that just because someone has more karma that someone else he is more experienced. Well, my program$$anonymous$$g experience is over 3 decades, which is much more program$$anonymous$$g experience than some people with many k's of karma. Yet, @Alanj2007 directs his question to others.
BTW, although I agree that it's not very good practice to name classes the same as well known methods (can be confusing), still it's not an issue here: the well known LoadLevel is a member of the Application class, as in Application.LoadLevel, while @Alanj2007 defines a new class LoadLevel. This would be a problem only of Unity had a LoadLevel class defined.
Good stuff. If scene and project is saved, try a reload too :) After loading Unity make sure to reload scene, just to make sure everything is updating in inspector.
Well I didn't look at it that way, but if it were that way, I totally agree with you. Clunk47 callout deleted, problem solved lol :)
Answer by fafase · Oct 26, 2013 at 04:54 PM
Here is my answer.
If there is anything on your file copy paste on notepad or else. Delete the script, create a new one with same name.
I had this issue before and that solved it.