an object reference is required to access non-static member unity.. problem :(

public class MainMenu : MonoBehaviour {

    public Texture playsprite;
    public float playtextureY;
    public float playtextureX;
    public Texture clickedplaysprite;

    void OnGUI(){
        //animator = GetComponent<Animator> ();


        if (GUI.Button(newRect(Screen.width/2.7f,Screen.height/3,Screen.width/4,Screen.height/10),playsprite,"")){
            playsprite=clickedplaysprite;
            Darkfadeinout.EndScene();

            //Application.LoadLevel(1);
    
        }

}
}

And here’s my code which should on clicking the playsprite fade screen into black and loadlevel 1 appears problem like in title…
An object reference is required to access non-static member `Darkfadeinout.EndScene()’

public class Darkfadeinout : MonoBehaviour {

    public float fadeSpeed = 1.5f;
    private bool sceneStarting = true;

public void EndScene(){
        guiTexture.enabled = true;
        FadeToBlack ();

        if(guiTexture.color.a >= 0.95f)
        {
            Application.LoadLevel(1);

}
    }
}

Please help ;…<

Please use code tags:

1 Like

You are accessing a method like you would a static class and as message says your class is not static.
Darkfadeinout.EndScene();
Either make your Darkfadeinput class static or add a reference to the class Darkfadeinput in your MainMenu class:

1 Like

Something like this ?

public static  Darkfadeinout darknessscript;


    void OnGUI(){
        //animator = GetComponent<Animator> ();

        if (GUI.Button(new Rect(Screen.width/2.7f, Screen.height/3, Screen.width/4,Screen.height/10),playsprite,"")){
        
        
            playsprite=clickedplaysprite;
            print ("clicked start game");
            darknessscript.EndScene();

Now on clicking sprite Console says ā€˜Object reference not set to an instance of an object’ :<

Other way i changed darkfadeinout class on public static class and Console says ā€˜Static class Darkfadeinout' cannot derive from type UnityEngine.MonoBehaviour’. Static classes must derive from object’ :confused:

In the MainMenu class you want to add the line…

private Darkfadeinout darkFadeScript = new Darkfadeinout();

This creates an instance of the Darkfadeinout class (script) in your MainMenu class (script) so that you can access its methods/functions.

So then line 14 would now read…

darkFadeScript.EndScene();

I did it, but now Console says
ā€˜NullReferenceException
Darkfadeinout.EndScene () (at Assets/Darkfadeinout.cs:33)
MainMenu.OnGUI () (at Assets/MainMenu.cs:29)’
:<

Or you could make your class/function static

desnt help :confused:

Last time I checked, you cannot use new with a mono behavior. Get rid of new statement, make the variable public or use serialize able atrribute, add darkfade to game object, and drag drop it in inspector or use get component.

You can technically do it, it just throws up a warning…

Also…the fix it just to remove the Monobehaviour derivative from the class since he’s not attaching it to a GameObject.

I’d need to see what you typed in the code to see why you’re getting a ā€œNullReferenceExceptionā€.

using UnityEngine;
using System.Collections;

     public class Darkfadeinout : MonoBehaviour {

    public float fadeSpeed = 1f;
    private bool sceneStarting = true;
    void Awake(){
                guiTexture.pixelInset = new Rect (0f, 0f, Screen.width, Screen.height);
        }
    void Update()
    {
                if (sceneStarting) {
                        StartScene();
                }
        }
    void FadeToClear(){
                guiTexture.color = Color.Lerp (guiTexture.color, Color.clear, fadeSpeed * Time.deltaTime);
        }
     void FadeToBlack(){
                guiTexture.color = Color.Lerp (guiTexture.color, Color.black, fadeSpeed * Time.deltaTime);
    }
    void StartScene(){
        FadeToClear ();
        if(guiTexture.color.a <= 0.05f)
        {
            guiTexture.color=Color.clear;
            guiTexture.enabled = false;
            sceneStarting = false;
        }
}
    public void EndScene(){
        guiTexture.enabled = true;
        FadeToBlack ();

        if(guiTexture.color.a >= 0.95f)
        {
            Application.LoadLevel(1);

}
    }
}
using UnityEngine;
using System.Collections;

public  class MainMenu : MonoBehaviour {

    public Texture playsprite;
    public float playtextureY;
    public float playtextureX;
    public Texture clickedplaysprite;

    public Texture soundsprite;
    public float soundtextureY;
    public float soundtextureX;

    public Texture bgtexture;

    private  Darkfadeinout darknessscript = new Darkfadeinout();


    void OnGUI(){
        //animator = GetComponent<Animator> ();

 
        if (GUI.Button(new Rect(Screen.width/2.7f, Screen.height/3, Screen.width/4,Screen.height/10),playsprite,"")){
          

            playsprite=clickedplaysprite;
            print ("clicked start game");
          //  Application.LoadLevel(1);
            darknessscript.EndScene();


     
        }
        if (GUI.Button(new Rect (Screen.width / soundtextureX, Screen.height / soundtextureY, Screen.width / 5,Screen.height/ 5),soundsprite, "")){
            print ("clicked sound ");
     
        }
}

/*    void Update() {
                darknessscript.EndScene ();
        }*/
}

here u got both scripts… still null reference exception :<

Well this doesn’t have anything to do with your NullReferenceException, but you call the ā€œdarknessscript.EndScene();ā€ after you called ā€œApplication.LoadLevel(1);ā€. Unless I’m mistaken, as soon as it executes the LoadLevel it’s not longer going to execute anything after that, because everything in the current level gets destroyed (unless it’s marked ā€œDontDestroyOnLoadā€).

Also, ā€œdarknessscript.EndScene();ā€ already has a line in it that does ā€œApplication.LoadLevel(1);ā€.

Yeah i know. I didn’t see that. I wrote application load level cause darknessscript. End scene didn’t work. And for now is like that. There was no application load level when I was posting. Sorry. I want it to work with end scene without application load level