Question by
actionjdjackson · Apr 29, 2020 at 07:05 PM ·
c#inheritance
How Do I Set A Parent Classes' Variables and override their functions
public class NPC : MonoBehaviour
{
public Rigidbody2D myRigidBody;
public Animator animator;
public AudioSource audioSource;
public GameObject skillBar;
public char[] moveActions = { 'I', 'U', 'D', 'L', 'R', 'S' };
public char currentAction = 'I';
void Start()
{
myRigidBody = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
audioSource = GetComponent<AudioSource>();
skillBar = GameObject.Find("SkillBar");
}
void OnCollisionEnter2D(Collision2D collider) {
if (collider.gameObject.name == "Player")
{
skillBar.SetActive(false);
}
}
I want to inherit from NPC as such:
public class InnKeeperSally : NPC
{
moveActions = { 'I' };
currentAction = 'I';
public void Start() {
myRigidBody = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
audioSource = GetComponent<AudioSource>();
skillBar = GameObject.Find("SkillBar");
if (currentAction == 'I')
{
animator.SetBool("WalkingLeft", false);
animator.SetBool("WalkingRight", false);
animator.SetBool("WalkingDown", false);
animator.SetBool("WalkingUp", false);
animator.SetBool("Distressed", false);
audioSource.Pause();
}
}
public void OnCollisionEnter2D(Collision2D collider)
{
if (collider.gameObject.name == "Player")
{
skillBar.SetActive(false);
VP_DialogManager.SendDialogMessage("meetInnKeeperSally");
inConversation = true;
currentAction = 'I';
}
}
I want to be able to change the char[] moveActions that is inherited from the NPC, and the char currentActions. I also want to override OnCollisionEnter2D to include a function that calls up a dialogue box. I get errors about this:
The same field name is serialized multiple times in the class or its parent class. This is not supported: Base(InnKeeperSally) currentAction
The same field name is serialized multiple times in the class or its parent class. This is not supported: Base(MonoBehaviour) moveActions UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)
I need help!
Comment
Best Answer
Answer by Hellium · Apr 29, 2020 at 09:16 PM
public class NPC : MonoBehaviour
{
protected Rigidbody2D myRigidBody;
protected Animator animator;
protected AudioSource audioSource;
protected GameObject skillBar;
public char[] moveActions;
public char currentAction;
protected virtual void Start()
{
myRigidBody = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
audioSource = GetComponent<AudioSource>();
skillBar = GameObject.Find("SkillBar");
}
private void OnCollisionEnter2D(Collision2D collider)
{
if (collider.gameObject.name == "Player")
OnCollisionWithPlayer();
}
protected virtual void OnCollisionWithPlayer()
{
skillBar.SetActive(false);
}
// Will be called when attaching the component or right clicking on the component > Reset
protected virtual void Reset()
{
moveActions = new char[]{ 'I', 'U', 'D', 'L', 'R', 'S' };
currentAction = 'I';
}
}
public class InnKeeperSally : NPC
{
protected override void Start()
{
base.Start():
if (currentAction == 'I')
{
animator.SetBool("WalkingLeft", false);
animator.SetBool("WalkingRight", false);
animator.SetBool("WalkingDown", false);
animator.SetBool("WalkingUp", false);
animator.SetBool("Distressed", false);
audioSource.Pause();
}
}
protected override void OnCollisionWithPlayer()
{
base.OnCollisionWithPlayer();
VP_DialogManager.SendDialogMessage("meetInnKeeperSally");
inConversation = true;
currentAction = 'I';
}
private void Reset()
{
moveActions = new char[]{ 'I' };
currentAction = 'I';
}
}