Nested Class access parent variable

I have a Nested Class and in it I am now trying to access a variable of the parent.
Incorrect example:

private class MyClass: MonoBehaviour
{
    private SpriteRenderer _Renderer;
    private class MySubClass
     {
            _Renderer.color = Color.Red;
     }
}

How can I do this?

Make the private variable protected instead. Then its derivative classes will be able to access it!

[EDIT] Sorry, I thought the second class was inheriting from the first. I jumped the gun! You’ll need to make the _Renderer variable static, then change its color by referencing it through the first class.

public class C1
    {
        protected static SpriteRenderer sr;
        private class C2
        {
            void ChangeColor()
            {
                C1.sr.color = Color.red;
            }

        }
    }

Otherwise, have the second class inherit the parent (it can’t be nested within the parent to do this, however) like so:

public class C1
    {
        protected SpriteRenderer sr;
    }

    public class C2 : C1
    {
        void ChangeColor()
        {
            sr.color = Color.red;
        }
    }
1 Like

What do you mean by the derived class?

As I’d misunderstood the situation originally, I thought your SubClass was inheriting from MyClass. That would make your SubClass derivative of MyClass. This means that it would inherit all of MyClass’s members and methods. As SubClass is, in fact, not inheriting from MyClass, it is not a derivative, though it should be able to access static members of MyClass.

Try this out and see if it works!:

public class MyClass : MonoBehaviour
    {
        private static SpriteRenderer _Renderer;
        private class MySubClass
        {
            public static void ChangeColorToRed()
            {
                _Renderer.color = Color.red;
            }
        }
    }
1 Like

Thanks for the help :slight_smile:

1 Like

Works.
How can I start the StartCoroutine function?

You can call that from anywhere within the MonoBehaviour script you want to run the coroutine. For example:

    public class ExampleClass : MonoBehaviour
    {
        private void Start()
        {
            StartCoroutine(Coroutine());
        }

        private IEnumerator Coroutine()
        {
            // Do something
            yield return null;
        }
    }

This’ll start the coroutine called Coroutine up. Make sure your coroutines have a return type of IEnumerator, and that they have a yield statement somewhere in there. Additionally, try to make sure you aren’t running the same coroutine more than once accidentally! That can lead to some funky situations.

Ok. And how does that work in a nested class?

Only MonoBehaviours can run coroutines, BUT the nested class can have a coroutine accessible to a MonoBehaviour that can execute the code itself. So this would work:

    public class MyClass : MonoBehaviour
    {
        private class MySubClass
        {
            public static IEnumerator Coroutine()
            {
                yield return null;
            }
        }

        private void Start()
        {
            StartCoroutine(MySubClass.Coroutine());
        }
    }

Notice that the coroutine needs to be static for us to access it through the class. Otherwise we’d need to get an instance of MySubClass and run that instance’s copy of the Coroutine method, like so:

    public class MyClass : MonoBehaviour
    {
        private class MySubClass
        {
            public IEnumerator Coroutine()
            {
                yield return null;
            }
        }

        private MySubClass subClass = new MySubClass();

        private void Start()
        {
            StartCoroutine(subClass.Coroutine());
        }
    }
1 Like