如何从 1 个脚本中制作一个公共的“enemyDamage”整数,与另一个脚本通信?

How can I make a public "enemyDamage" integer from 1 script, communicate with another script?

提问人:HarrisonO 提问时间:7/18/2020 更新时间:7/19/2020 访问量:55

问:

我是编码新手,不太确定如何将上下文从一个脚本添加到另一个脚本。到目前为止,对于我的游戏,我一直在从多个来源编写科学怪人代码,并且已经陷入了死胡同。我希望我的玩家受到与“敌人”脚本中 enemyDamage 所述的伤害相同的伤害。我不太确定该给你什么其他背景,但如果你知道如何帮助我,那就太棒了!

敌人脚本

using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;

public class Enemy : MonoBehaviour
{
    public int health = 100;
    public int enemyDamage = 10;
    public GameObject deathEffect;

    public void TakeDamage (int damage)
    {
        health -= damage;

        if (health <= 0)
        {
            Die();
        }
    }
   void Die ()
    {
        Instantiate(deathEffect, transform.position, Quaternion.identity);
        Destroy(gameObject);
    }
    
}

PlayerHealth 脚本

    using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;

public class PlayerHealth : MonoBehaviour
{
    public int maxHealth = 10;
    public int currentHealth;
    public int damage = 1;
    public HealthBar healthBar;
    // Start is called before the first frame update
    void Start()
    {
        currentHealth = maxHealth;
        healthBar.SetMaxHealth(maxHealth);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.R))
        {
            TakenDamage(1);
        }

        if (currentHealth <= 0)
        {
            PlayerDeath();
        }
    }
    public void TakenDamage(int damage)
    {
        currentHealth -= damage;

        healthBar.SetHealth(currentHealth);
    }
    void PlayerDeath()
    {
        UnityEngine.Debug.Log("bean guy");
    }
    public void OnTriggerEnter2D(Collider2D hitInfo)
    {
        UnityEngine.Debug.Log("We did it boys");
        PlayerHealth player = hitInfo.GetComponent<PlayerHealth>();
        { 
            UnityEngine.Debug.Log("beans");
            TakenDamage(enemyDamage); // I need this to update with enemyDamage 's value
        }



    }
}
C# Unity-游戏引擎

评论


答:

-1赞 IndieGameDev 7/18/2020 #1

有多种方法:

将 EnemyDamage 设为静态变量

public static int enemyDamage = 10;

然后,您可以在其他脚本中调用它Enemy.enemyDamage

请注意,您不能在检查器中设置静态变量。

使用 GetComponent

Enemy enemy = gameObject.GetComponent(typeof(Enemy )) as Enemy;

enemy.enemyDamage 

创建 GameManager

游戏管理器 .CS:

#region Singelton
    public static GameManager instance;

    void Awake()
    {
        if (instance != null)
        {
            Debug.LogWarning("More than one Instance of Inventory found");
            return;
        }

        instance = this;
    }
#endregion
    
public int enemyDamage = 10;    

引用 GameManager 脚本:

GameManager gm;

void Start()
    {
        gm = GameManager.instance;
    }

//Calling Function in GameManager
gm.EndGame();
// Getting Value from GameManager
gm.enemyDamage();

什么时候使用什么?

  • 如果你想要一个更短期的解决方案,我会使用一个静态变量,不建议使用多个敌人(不同的敌人伤害值现在是不可改变的)
  • 如果您有更多变量甚至需要从多个脚本访问的函数,我建议您改用游戏管理器
  • 您需要获取敌人的引用才能使用 GetComponent,但可以添加多个不同的 enemyDamage 值

评论

0赞 Everts 7/19/2020
静态不是组件交互的解决方案。EneymyDamage 现在在所有敌人之间共享,并且只有一个版本存在。这意味着你不能有一个弱者和强的敌人,他们都造成 10 点伤害。这个问题已经被问过很多次了,答案是GetComponent。在这种情况下,静态是不对的。
0赞 IndieGameDev 7/19/2020
根据您的反馈编辑了我的帖子。