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:
public class Character {
private int currentHP;
public Character(){}
public void Attack(){
// does nothing right now
}
public void TakeDamage(int damage){
currentHP -= damage;
}
public static void SomeFunction(){
// exists only for the example at the end of this post. Note the "static" modifier in the method declaration
}
}
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:
Character a = new Character();
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:
Your code with an object reference added
public class Anglemastery : MonoBehaviour
{
[SerializeField] private Optiopane2 optioPane; // assign through the inspector
public bool tester;
public float angleconfig(float a)
{
if (tester == false)
return a;
else
{
string n = optiopane.getAngleSettings(); // note how we use the reference here!
switch (n)
{
case "Radian":
return a;
break;
case "Degree":
a = a * (180/Mathf.PI);
return a;
break;
case "Gradian":
a = a * (200 / Mathf.PI);
return a;
break;
}
}
}
}
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.