Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by misiekkox, Jan 15, 2015.

  1. misiekkox

    misiekkox

    Joined:
    Jan 10, 2015
    Posts:
    14
    Code (CSharp):
    1. public class MainMenu : MonoBehaviour {
    2.  
    3.     public Texture playsprite;
    4.     public float playtextureY;
    5.     public float playtextureX;
    6.     public Texture clickedplaysprite;
    7.  
    8.     void OnGUI(){
    9.         //animator = GetComponent<Animator> ();
    10.  
    11.  
    12.         if (GUI.Button(newRect(Screen.width/2.7f,Screen.height/3,Screen.width/4,Screen.height/10),playsprite,"")){
    13.             playsprite=clickedplaysprite;
    14.             Darkfadeinout.EndScene();
    15.  
    16.             //Application.LoadLevel(1);
    17.    
    18.         }
    19.  
    20. }
    21. }
    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()'

    Code (CSharp):
    1. public class Darkfadeinout : MonoBehaviour {
    2.  
    3.     public float fadeSpeed = 1.5f;
    4.     private bool sceneStarting = true;
    5.  
    6. public void EndScene(){
    7.         guiTexture.enabled = true;
    8.         FadeToBlack ();
    9.  
    10.         if(guiTexture.color.a >= 0.95f)
    11.         {
    12.             Application.LoadLevel(1);
    13.  
    14. }
    15.     }
    16. }
    Please help ;...<
     
    Last edited: Jan 15, 2015
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  3. shotoutgames

    shotoutgames

    Joined:
    Dec 29, 2013
    Posts:
    283
    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:
     
    Jessy likes this.
  4. misiekkox

    misiekkox

    Joined:
    Jan 10, 2015
    Posts:
    14
    Something like this ?

    Code (CSharp):
    1. public static  Darkfadeinout darknessscript;
    2.  
    3.  
    4.     void OnGUI(){
    5.         //animator = GetComponent<Animator> ();
    6.  
    7.         if (GUI.Button(new Rect(Screen.width/2.7f, Screen.height/3, Screen.width/4,Screen.height/10),playsprite,"")){
    8.        
    9.        
    10.             playsprite=clickedplaysprite;
    11.             print ("clicked start game");
    12.             darknessscript.EndScene();
     
  5. misiekkox

    misiekkox

    Joined:
    Jan 10, 2015
    Posts:
    14
    Now on clicking sprite Console says 'Object reference not set to an instance of an object' :<
     
    Last edited: Jan 15, 2015
  6. misiekkox

    misiekkox

    Joined:
    Jan 10, 2015
    Posts:
    14
    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' :/
     
  7. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    682
    In the MainMenu class you want to add the line...

    Code (CSharp):
    1. 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...

    Code (CSharp):
    1. darkFadeScript.EndScene();
     
  8. misiekkox

    misiekkox

    Joined:
    Jan 10, 2015
    Posts:
    14
    I did it, but now Console says
    'NullReferenceException
    Darkfadeinout.EndScene () (at Assets/Darkfadeinout.cs:33)
    MainMenu.OnGUI () (at Assets/MainMenu.cs:29)'
    :<
     
  9. AndreasGan

    AndreasGan

    Joined:
    Feb 27, 2014
    Posts:
    16
    Or you could make your class/function static
     
  10. misiekkox

    misiekkox

    Joined:
    Jan 10, 2015
    Posts:
    14
    desnt help :/
     
  11. Random_Civilian

    Random_Civilian

    Joined:
    Nov 5, 2014
    Posts:
    55
    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.
     
  12. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    682
    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".
     
  13. misiekkox

    misiekkox

    Joined:
    Jan 10, 2015
    Posts:
    14
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.      public class Darkfadeinout : MonoBehaviour {
    5.  
    6.     public float fadeSpeed = 1f;
    7.     private bool sceneStarting = true;
    8.     void Awake(){
    9.                 guiTexture.pixelInset = new Rect (0f, 0f, Screen.width, Screen.height);
    10.         }
    11.     void Update()
    12.     {
    13.                 if (sceneStarting) {
    14.                         StartScene();
    15.                 }
    16.         }
    17.     void FadeToClear(){
    18.                 guiTexture.color = Color.Lerp (guiTexture.color, Color.clear, fadeSpeed * Time.deltaTime);
    19.         }
    20.      void FadeToBlack(){
    21.                 guiTexture.color = Color.Lerp (guiTexture.color, Color.black, fadeSpeed * Time.deltaTime);
    22.     }
    23.     void StartScene(){
    24.         FadeToClear ();
    25.         if(guiTexture.color.a <= 0.05f)
    26.         {
    27.             guiTexture.color=Color.clear;
    28.             guiTexture.enabled = false;
    29.             sceneStarting = false;
    30.         }
    31. }
    32.     public void EndScene(){
    33.         guiTexture.enabled = true;
    34.         FadeToBlack ();
    35.  
    36.         if(guiTexture.color.a >= 0.95f)
    37.         {
    38.             Application.LoadLevel(1);
    39.  
    40. }
    41.     }
    42. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public  class MainMenu : MonoBehaviour {
    5.  
    6.     public Texture playsprite;
    7.     public float playtextureY;
    8.     public float playtextureX;
    9.     public Texture clickedplaysprite;
    10.  
    11.     public Texture soundsprite;
    12.     public float soundtextureY;
    13.     public float soundtextureX;
    14.  
    15.     public Texture bgtexture;
    16.  
    17.     private  Darkfadeinout darknessscript = new Darkfadeinout();
    18.  
    19.  
    20.     void OnGUI(){
    21.         //animator = GetComponent<Animator> ();
    22.  
    23.  
    24.         if (GUI.Button(new Rect(Screen.width/2.7f, Screen.height/3, Screen.width/4,Screen.height/10),playsprite,"")){
    25.          
    26.  
    27.             playsprite=clickedplaysprite;
    28.             print ("clicked start game");
    29.           //  Application.LoadLevel(1);
    30.             darknessscript.EndScene();
    31.  
    32.  
    33.      
    34.         }
    35.         if (GUI.Button(new Rect (Screen.width / soundtextureX, Screen.height / soundtextureY, Screen.width / 5,Screen.height/ 5),soundsprite, "")){
    36.             print ("clicked sound ");
    37.      
    38.         }
    39. }
    40.  
    41. /*    void Update() {
    42.                 darknessscript.EndScene ();
    43.         }*/
    44. }
    here u got both scripts... still null reference exception :<
     
    Last edited: Jan 18, 2015
  14. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    682
    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);".
     
  15. misiekkox

    misiekkox

    Joined:
    Jan 10, 2015
    Posts:
    14
    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