How can Internal variable get accessed from Editor script?

Hello all,

I have a normal script (not editor script)

public class InternalTest : MonoBehaviour
{
    internal float myVariable;
}

and an editor script

[CustomEditor(typeof(InternalTest))]
public class InternalTestEditor : Editor
{
    new InternalTest target;

    void OnEnable()
    {
        target = (InternalTest)base.target;
    }

    public override void OnInspectorGUI()
    {
        target.myVariable = Random.Range(0, 100);
    }
}

Now, I get the following: ‘InternalTest.myVariable’ is inaccessible due to its protection level

So, can’t I access myVariable from the editor script? Maybe because Editor scripts are a different assembly?

Thanks in advance!

Yes your guess is right. What should you do now? Well you could either make “myVariable” public or move InternalTestEditor outside of “Editor” folder and wrap your code like that:

#if UNITY_EDITOR
[CustomEditor(typeof(InternalTest))]
public class InternalTestEditor : Editor
 {
     new InternalTest target;
 
     void OnEnable()
     {
         target = (InternalTest)base.target;
     }
 
     public override void OnInspectorGUI()
     {
         target.myVariable = Random.Range(0, 100);
     }
 }
#endif

The choice is up to you.