c# doesn't behave correctly

-edit- Obvious typo caused incorrect results. Should probably ignore.

Firstly my console app, compiled in visual studios 2010.

using System;

internal class Program
{
    private static void Main(string[] args)
    {
        new Window1().ProcessInput();
    }
}

public abstract class Window
{
    public bool IsActive = false;

    public virtual void ProcessInput()
    {
        Console.WriteLine("before");
        if (!IsActive) return;
    }

}

public class Window1 : Window
{
    public override void ProcessInput()
    {
        base.ProcessInput();
        Console.WriteLine("after");
    }
}

output

Now implemented in unity

Window.cs

using UnityEngine;

public abstract class Window : MonoBehaviour
{
    public bool IsActive;

    void Start()
    {
        WindowManager.Instance.AddWindow(this);
    }

    public virtual void ProcessInput()
    {
        Debug.Log("before");
        if (!IsActive) return;
    }
}

window1.cs

using UnityEngine;

public class Window1 : Window
{
    public override void ProcessInput()
    {
        base.ProcessInput();
        Debug.Log("after");
    }
}

WindowManager.cs

using UnityEngine;
using System.Collections.Generic;

public class WindowManager : MonoBehaviour
{
    private readonly Queue<Window> _windows = new Queue<Window>();

    public void Start()
    {
        _windows.Peek().ProcessInput();
    }

    public void AddWindow(Window window)
    {
        _windows.Enqueue(window);
    }

    #region Singleton
    private static WindowManager _instance;
    public static WindowManager Instance
    {
        get
        {
            if (_instance == null)
            {
                var go = new GameObject();
                _instance = go.AddComponent<WindowManager>();
                go.name = "WindowManager";
            }

            return _instance;
        }
    }

    #endregion
}

output

So the return only returns from the base, not the whole function unlike in the console app.
Before someone says something, IsActive is false in inspector, I checked multiple times.

What I’d like to know is there any easy way to have this behave how I want it that I haven’t thought of, without having to add the if(!IsActive) return; to every class that inherits Window.

I know it’s not a big deal but I’m kind of a perfectionist especially when I had such an elegant solution that doesn’t work anymore. Pretty much I don’t want to require someone that extends my code to have to add a specific line to make it work correctly.

var go = new GameObject(); if you want to use javascript, code it in a java file, you may get better results.

Thats valid C# 3.0 code which can be used in any Unity 3 version.
UnityScript since unity 3 has lost most of its benefit, its only area where its superior is missbehaviors and bugs

At the orig question: where is the rest of the code? This code there will never fire this if you create an instance of Window and add it and then use processinput so somewhere a Window1 component is being generated and added as the inheritance does not call “the other way round”

Window1 is just attached to an empty gameobject, then when app runs it’ll create windowmanager.

And in which way is the output then wrong?
Thats exactly what its meant to do as the ProcessInput is called on Window1, this will call the base ProcessInput which results in “Before” and then it will continue on the extended ProcessInput which will result in After.
Thats fully correct.

The console app code would show the same if you would have created the correct class there which is Window1, not Window or if you would have put a Window component into the scene, not the Window1 (putting the behavior into the scene is equivalent to using the constructor basically yet you don’t create the same class on both sides so the output will not be the same too)

Oh god, I can’t believe I missed such an obvious typo. I checked it multiple times.

Still doesn’t solve my problem, but I’ll just work something else out.

Well whats the exact problem then? Or what do you want to happen?

Do you want to make use of the IsActive in higher layers?

If so I would make ProcessInput return a bool and use a if (base.ProcessInput()) { rest here } style of usage for example. Then you can proctect it on the base and its ‘done’

At the time your problem is very hard to understand cause it makes totally no sense practially that the check is in there at all. if you return there or not couldn’t be of any less use as it will return there anyway :wink: (resharper in vs likely would mark the line as being without practical use as it does it too if you have an if { … return } else {} too for the else)

I wanted only the base class function to run if it wasn’t active, without checking IsActive on the class inheriting it. (For the record resharper doesn’t flag anything.)

It’s alright though I’ve already rewritten it and changed the logic so I don’t even an inactive property and also it doesn’t even inherit monobehaviour as I realized I don’t need it to create a GUI

I don’t understand you problem frankly… What do you want?
The output you have shown is correct, that is the way it should be…
Additionally it lacks the code where a window is added to the queue. So not much to say here…

I edited and updated the post