- Home /
Class Reference to Other Class does not allow me to use its Methods
This is "Weapon" class.
public class Weapon : MonoBehaviour
{
[SerializeField] protected Mode PrimaryMode;
[SerializeField] protected Mode SecondaryMode;
[...]
private void Update()
{
if (Input.GetKeyDown(KeyCode.Q))
{
Debug.Log(PrimaryMode);
PrimaryMode.useMode();
}
}
}
This is "Mode" Abstract Class
public abstract class Mode : MonoBehaviour
{
[SerializeField]
protected int Type;
[...]
public void useMode() { }
}
This is "FireMode" Subclass
public class FireMode : Mode
{
[...]
public new void useMode()
{
Debug.Log("HERE !");
}
}
Now this is the situation: if I hit "Q", the console reports "
Weapon 1 (FireMode) UnityEngine.Debug:Log(Object) Weapon:Update() (at Assets/_MyScripts/_XXX/Weapon.cs:25)
So you see the problem: Unity knows I'm referring to a FireMode class script, but it refuses to let me use "useMode()" Method of that class.
Does changing "public void use$$anonymous$$ode() {}" to "abstract void use$$anonymous$$ode();" in $$anonymous$$ode class help?
Also I don't get this "new" in "public new void use$$anonymous$$ode()" in Firemode...
Answer by RichardDelta93 · Nov 01, 2018 at 11:49 PM
Yes, it works now. Thank you.
I had to set "useMode" of Mode class abstract, also I had to add the keywork Override to "useMode" methods of subclasses.
Your answer
Follow this Question
Related Questions
Learning Unity Methods 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Classes and Scripting 2 Answers
Make custom editor for generic class to apply to all current and future child classes 1 Answer