Reputation: 319
So I am trying to create my own methods (like Update, Start etc) Basically, something that will detect input but with a bool method. More like an "OnKeyDown".
I get no errors but nothing happens, here are my classes:
using UnityEngine;
using System.Collections;
public class BeastScript : MonoBehaviour
{
void Update()
{
#region INPUT
//KEYS
if (Input.anyKeyDown)
{
OnKeyDown(Input.inputString);
}
if (Input.anyKey)
{
OnKeyHold(Input.inputString);
}
//MOUSE
if (Input.GetMouseButtonDown(0))
{
OnMouseClick(0);
}
if (Input.GetMouseButtonDown(1))
{
OnMouseClick(1);
}
if (Input.GetMouseButtonDown(2))
{
OnMouseClick(2);
}
if (Input.GetMouseButton(0))
{
OnMouseClickHold(0);
}
if (Input.GetMouseButton(1))
{
OnMouseClickHold(1);
}
if (Input.GetMouseButton(2))
{
OnMouseClickHold(2);
}
if (Input.GetMouseButtonUp(0))
{
OnMouseRelease(0);
}
if (Input.GetMouseButtonUp(1))
{
OnMouseRelease(1);
}
if (Input.GetMouseButtonUp(2))
{
OnMouseRelease(2);
}
#endregion INPUT
}
public virtual void OnKeyDown(string key) { }
public virtual void OnKeyHold(string key) { }
public virtual void OnMouseClick(int button) { }
public virtual void OnMouseClickHold(int button) { }
public virtual void OnMouseRelease(int button) { }
}
Here is the derived
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using Beast;
using Beast.BUI;
using Beast.Extensions;
using System;
using UnityEngine.Events;
using Beast.Web;
public class Tester : BeastScript {
void Start()
{
}
void OnGUI ()
{
}
void Update ()
{
}
public override void OnMouseClick(int button)
{
base.OnMouseClick(button);
if (button == 0)
{
print("Works");
}
}
}
Upvotes: 1
Views: 5879
Reputation: 3629
When inheriting a class which's methods you want to override, you have to declare these methods virtual. In your example, both classes have a private void Update()
(when not declaring an access modifier it is implicitly private).
This means, the inheriting class (Tester) does not even know that its base class already has an Update() method. Thus it will not override it, but hide it. Once Unity calls the Update() method of Tester, it will have absolutely no effect, as it's empty.
What you want is to have Tester's Update() method to include all the contents of BeastScript's Update() method, and optionally extend it. That can be accomplished like this:
public class BeastScript : MonoBehaviour
{
protected virtual void Update() // "protected" so subclasses can see it
{ // and "virtual" so subclasses can override it
if (Input.anyKeyDown)
...
}
}
public class Tester : BeastScript
{
protected override Update() // "protected" because overriding methods must not be less accessible than the method they override
{ // and "override" to specify that this method overrides a virtual method
// optional: do stuff before calling BeastScript.Update()
base.Update(); // this calls BeastScript.Update()
// optional: do stuff after calling BeastScript.Update()
}
}
Upvotes: 1
Reputation: 3907
Make sure to call base.Update()
if you are running script in the base class method Update
.
void Update ()
{
base.Update();
// special Update code
}
Upvotes: 3
Reputation: 1346
You made an empty Update method that replaced the base Update method. Remove it in Tester class
Upvotes: 0