Skip to main content
added 20 characters in body
Source Link
jzx
  • 3.8k
  • 2
  • 26
  • 38

The Update in the derived class should be an override of a protected virtual Update in the base class.

Imagine when Unity reflects the non-public members on your behavior -

BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic;
MethodInfo theMethod = typeof(Lamp).GetMethod("Update", flags);
theMethod.Invoke(lampInstance);

Which Update method gets called? By having private and a protected methods that aren't associated as an override, both are available so one will be chosen depending on the underlying implementation - possibly throwing an exception.

The proper way to implement this pattern would be:

public class BlinkingBehaviour : MonoBehaviour
{
    protected virtual void Update()
    {
        // ...
    }
}
public class Lamp : BlinkingBehaviour
{
    protected override void Update()
    {
        base.Update();
        // ...
    }
}

The Update in the derived class should be an override of a protected virtual Update in the base class.

Imagine when Unity reflects the non-public members on your behavior -

BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic;
MethodInfo theMethod = typeof(Lamp).GetMethod("Update", flags);
theMethod.Invoke(lampInstance);

Which Update method gets called? By having private and a protected methods that aren't associated as an override, both are available so one will be chosen depending on the underlying implementation - possibly throwing an exception.

The proper way to implement this pattern would be:

public class BlinkingBehaviour : MonoBehaviour
{
    protected virtual void Update()
    {
        // ...
    }
}
public class Lamp : BlinkingBehaviour
{
    protected override void Update()
    {
        base.Update();
    }
}

The Update in the derived class should be an override of a protected virtual Update in the base class.

Imagine when Unity reflects the non-public members on your behavior -

BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic;
MethodInfo theMethod = typeof(Lamp).GetMethod("Update", flags);
theMethod.Invoke(lampInstance);

Which Update method gets called? By having private and a protected methods that aren't associated as an override, both are available so one will be chosen depending on the underlying implementation - possibly throwing an exception.

The proper way to implement this pattern would be:

public class BlinkingBehaviour : MonoBehaviour
{
    protected virtual void Update()
    {
        // ...
    }
}
public class Lamp : BlinkingBehaviour
{
    protected override void Update()
    {
        base.Update();
        // ...
    }
}
Source Link
jzx
  • 3.8k
  • 2
  • 26
  • 38

The Update in the derived class should be an override of a protected virtual Update in the base class.

Imagine when Unity reflects the non-public members on your behavior -

BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic;
MethodInfo theMethod = typeof(Lamp).GetMethod("Update", flags);
theMethod.Invoke(lampInstance);

Which Update method gets called? By having private and a protected methods that aren't associated as an override, both are available so one will be chosen depending on the underlying implementation - possibly throwing an exception.

The proper way to implement this pattern would be:

public class BlinkingBehaviour : MonoBehaviour
{
    protected virtual void Update()
    {
        // ...
    }
}
public class Lamp : BlinkingBehaviour
{
    protected override void Update()
    {
        base.Update();
    }
}