Search Unity

Keep running into error error CS0120

Discussion in 'Scripting' started by JimmySur, Sep 18, 2019.

  1. JimmySur

    JimmySur

    Joined:
    Sep 18, 2019
    Posts:
    3
    I am still very new to Unity. (Have only been developing for 2 weeks now) I keep running into the error CS0120, " An object reference is required for the non-static field, method, or property 'Optionpane2.getAngleSettings()'" this only occurs with this script as seen here.

    I am attempting to set up a way to configure what angle measurements are displayed.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6.  
    7. public class Anglemastery : MonoBehaviour
    8. {
    9.     public bool tester;
    10.     public float angleconfig(float a)
    11.  
    12.     {
    13.    
    14.     if (tester == false)
    15.             return a;
    16.         else
    17.         {
    18.             string n = Optionpane2.getAngleSettings();
    19.             switch (n)
    20.             {
    21.                 case "Radian":
    22.                     return a;
    23.                     break;
    24.                 case "Degree":
    25.                     a = a * (180/Mathf.PI);
    26.                     return a;
    27.                     break;
    28.                 case "Gradian":
    29.                     a = a * (200 / Mathf.PI);
    30.                     return a;
    31.                     break;
    32.                  
    33.  
    34.             }
    35.         }
    36.     }
    37. }
    The method getAngleSettings() comes from this code
    (Which is not the troublesome one) and is attached to a dropdown menu.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Events;
    5. using UnityEngine.UI;
    6.  
    7. public class Optionpane2 : MonoBehaviour
    8. {
    9.     private string ans;
    10.     public void anglesetup(int index)
    11.     {
    12.         int n = index;
    13.      
    14.  
    15.         switch(n+1)
    16.         {
    17.             case 1:
    18.                 ans = "Radian";
    19.                 break;
    20.             case 2:
    21.                 ans = "Degree";
    22.                 break;
    23.             case 3:
    24.                 ans = "Gradian";
    25.                 break;
    26.             case 4:
    27.                 ans = "Revolution";
    28.                 break;
    29.         }
    30.  
    31.         Debug.Log("Unit "+ ans);
    32.     }
    33.     public string getAngleSettings()
    34.     {
    35.         return ans;
    36.     }
    37. }
    38.  
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    I don't see where you are declaring Optionpane2 anywhere.
     
  3. JimmySur

    JimmySur

    Joined:
    Sep 18, 2019
    Posts:
    3
    What do I do to declare Optionpane2?
    I am still new...
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Hi and welcome.
    As the error states, Optiopane2.getAngleSettings() is not static, and as such you need to provide an object reference.
    There are two ways to fix this: either provide an object reference, or make the function static.

    Let me explain what's happening. When you write the code for some object, you are effectively creating its "blueprint".
    Let's imagine you create a class for some very simple Character:
    Code (CSharp):
    1. public class Character {
    2.    private int currentHP;
    3.  
    4.    public Character(){}
    5.  
    6.    public void Attack(){
    7.        // does nothing right now
    8.    }
    9.  
    10.    public void TakeDamage(int damage){
    11.        currentHP -= damage;
    12.    }
    13.  
    14.    public static void SomeFunction(){
    15.        // exists only for the example at the end of this post. Note the "static" modifier in the method declaration
    16.    }
    17.  
    18. }
    In object oriented programming, you generally want to track the state of an object. For example, two different characters may have different values for their "current health", as one of them, for example, could have already taken some damage. So each instance of that class has its own state. Let's just imagine we created two characters a and b:
    Code (CSharp):
    1. Character a = new Character();
    2. Character b = new Character();
    The two characters represent different individuals, so it's important which of the two we are referencing when, for example, we want that specific character to attack something. Or in other words, it's important to realise that there is a difference between calling a.TakeDamage(5) and b.TakeDamage(5), since one of them may be dead after the attack, while the other is not, depending on the current state of their instance (in this example the potentially different currentHP values).
    On the other hand, you will probably agree that calling Character.TakeDamage(5) does not make a whole lot of sense, right? Which character would take damage? What if no character exists? But that's exactly what you are currently doing when you call Optiopane2.getAngleSettings()! So the compiler has no idea what you want him to do.

    To fix your error, you need to give your AngleMastery class a reference to the instance of the Optiopane2 class it should be using. You can simply do so by adding a variable to your AngleMastery class, and then assigning that through the inspector by pulling in your Optiopane2 gameobject. The code would look like this:
    Code (CSharp):
    1. public class Anglemastery : MonoBehaviour
    2. {
    3.     [SerializeField] private Optiopane2 optioPane; // assign through the inspector
    4.     public bool tester;
    5.     public float angleconfig(float a)
    6.     {
    7.  
    8.     if (tester == false)
    9.             return a;
    10.         else
    11.         {
    12.             string n = optiopane.getAngleSettings(); // note how we use the reference here!
    13.             switch (n)
    14.             {
    15.                 case "Radian":
    16.                     return a;
    17.                     break;
    18.                 case "Degree":
    19.                     a = a * (180/Mathf.PI);
    20.                     return a;
    21.                     break;
    22.                 case "Gradian":
    23.                     a = a * (200 / Mathf.PI);
    24.                     return a;
    25.                     break;
    26.             }
    27.         }
    28.     }
    29. }
    30.  
    This allows us to reference an instance of Optiopane2, and thus to call optiopane.getAngleSettings(), which makes sense, instead of Optiopane2.getAngleSettings(), which does not make sense. Now the compiler actually knows what Optiopane2 instance you are talking about and thus does not throw an error anymore.

    Now, i mentioned there were two ways to fix this problem. The other way would have you make the method itself static, which allows it to be called from the class itself. In our character example above, it is possible to call Character.SomeFunction(), because SomeFunction() is declared as static. Note, however, that a static function can not work with anything that is not static itself (since that would again lead to the problem of the unknown instance to work with), so going this route, you would have to make "ans" static as well.

    Edits: Edited it a bit to make it easier to follow and easier to read. Hope the explanations are fine now. If you have any questions, feel free to ask.
    If you only want a solution, i basically already posted that as well in the spoiler (dont forget to set the reference in the inspector). However, i highly suggest you actually try to follow the examples and understand why the error occurs. Understanding the difference between static and non-static data in programming is very useful.
     
    Last edited: Sep 19, 2019
    JimmySur likes this.
  5. JimmySur

    JimmySur

    Joined:
    Sep 18, 2019
    Posts:
    3
    Thanks, I shall try that, and tell you how it went.

    Worked like a charm.
     
    Last edited: Sep 19, 2019