Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取基于回合的RPG的当前目标数据_C#_Unity3d - Fatal编程技术网

C# 获取基于回合的RPG的当前目标数据

C# 获取基于回合的RPG的当前目标数据,c#,unity3d,C#,Unity3d,我正在创造一个基于回合的战斗RPG,灵感来源于神性:原罪、D&D等等。目前,我有一个Game Object,其中包含场景中当前的所有角色,而这些角色中的每一个都只是一个Game Object,它有一个模型(目前只有一个立方体…)和一个character Stats组件,它就是这个脚本: public class CharacterStats : MonoBehaviour { public string characterName; public int health;

我正在创造一个基于回合的战斗RPG,灵感来源于神性:原罪、D&D等等。目前,我有一个
Game Object
,其中包含场景中当前的所有角色,而这些角色中的每一个都只是一个
Game Object
,它有一个模型(目前只有一个立方体…)和一个character Stats组件,它就是这个脚本:

 public class CharacterStats : MonoBehaviour {

    public string characterName;
    public int health;
    public int strength;
    public int dexterity;
    public int intelligence;

    // Use this for initialization
    void Start () {
        health = 100;
        strength = 10;
        dexterity = 10;
        intelligence = 10;
    }

    // Update is called once per frame
    void Update () {

    }
}
在角色的实际模型上(同样,现在只是一个立方体),我有以下用户交互脚本:

public class UserInteraction : MonoBehaviour {

    CharacterStats currentCharacterStats { get; set; }
    GameObject currentTargetCharatcerStats;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    void OnMouseDown()
    {
        currentCharacterStats = this.gameObject.GetComponentInParent<CharacterStats>();
        Debug.Log(this.gameObject.GetComponentInParent<CharacterStats>());
        Debug.Log(currentCharacterStats.characterName + " Clicked");

        //assing global taget stats
        var currentTarget = GameObject.Find("CurrentTarget");
        var currentTargetStats = currentTarget.GetComponent<CharacterStats>();
        currentCharacterStats = currentTargetStats;

    }    
}
我听说更新游戏对象是一个非常昂贵的任务,所以我想在游戏中添加更多系统之前尝试解决这个问题。谢谢下面是我的场景的当前结构:


是的,用
更新
方法进行操作不是一个好主意。
一种解决方案是将您的
CurrentTarget
类设置为单例

public class CurrentTarget : MonoBehaviour {

    static public CurrentTarget instance;
    // ...

    void Awake() {
        instance = this;
    }
这样,在调用
OnMouseDown
时,您可以执行以下操作:

CurrentTarget.instance.updateTarget(currentTargetStats);

然后您只需要创建
updateTarget
方法,该方法将
CharacterStats
对象作为参数,并对用户界面进行必要的更改(例如
healthText.text=“Health:+currentTarget.Health;
等)。

这是一个很好的解决方案,谢谢!我要试一试。我真傻,没先想到那件事。。
CurrentTarget.instance.updateTarget(currentTargetStats);