覆盖功能不覆盖(Csharp)

时间:2017-11-24 18:48:32

标签: c# override

我正处于游戏中,我一直在创建转换脚本。我在Unity中创建了一个基础Interact虚拟void,其中每个可交互的将使用公共覆盖void Interact()并执行所需的任何操作。我的问题是,当我在一个脚本中调用Interact()时,只调用main函数而不是覆盖函数。我正在使用Unity 2017,Csharp。

基本互动脚本

using UnityEngine.AI;
using UnityEngine;

public class Interactable : MonoBehaviour {

private NavMeshAgent NavAgent;
private bool isInteracting;

public virtual void MoveToLocation(NavMeshAgent navMesh)
{
    isInteracting = false;
    this.NavAgent = navMesh;
    NavAgent.stoppingDistance = 4;
    NavAgent.destination = transform.position;
}

private void Update()
{
    if(!isInteracting && NavAgent != null && !NavAgent.pathPending)
    {
        if(NavAgent.remainingDistance <= NavAgent.stoppingDistance)
        {
            Interact();
            Debug.Log("Moving To Interactable");
            isInteracting = true;
        }
        else
        {
            isInteracting = false;
        }
    }
}

public virtual void Interact()
{
    Debug.Log("herro");
}
}

NPC互动脚本

using UnityEngine.UI;

using UnityEngine;

public class NPC : Interactable {

//Setting Up Conversation Var
public string NPCName;
public string[] Conversation;
private int currentConversation;

//Gettig=ng UI
public CanvasGroup ConversationUI;
public Text Title;
public Text Text;

private void Start()
{
    currentConversation = 0;
}

public override void Interact()
{
    Debug.Log("Interacting With NPC");
    //Opening UI
    ConversationUI.alpha = 1;
    ConversationUI.interactable = true;
    ConversationUI.blocksRaycasts = true;
    //Setting Up Name
    Title.text = NPCName;
    Text.text = Conversation[currentConversation];
}

public void NextConv()
{
    currentConversation++;
    if (currentConversation >= Conversation.Length)
    {
        ConversationUI.alpha = 0;
        ConversationUI.interactable = false;
        ConversationUI.blocksRaycasts = false;
    }
    else if (currentConversation < Conversation.Length)
    {
        Text.text = Conversation[currentConversation];
    }
}
}

0 个答案:

没有答案