我正在尝试使用统一网络创建多人游戏。我创建了两个预制对象,一个是“玩家”预制对象,另一个是“子弹”预制对象。附带“播放器”预制件的健康脚本,以及类似的附带“子弹”预制件的项目符号脚本。但是,当我尝试从“子弹”预制件访问“播放器”预制件中的公共方法时,我遇到了一个问题。
请帮助我如何解决它。
// :::::: Health脚本:::::
using UnityEngine;
public class health : MonoBehaviour
{
public const int maxHealth = 100;
public int currentHealth = maxHealth;
public void takeDamage(int amount)
{
currentHealth -= amount;
if (currentHealth <= 0)
{
currentHealth = 0;
Debug.Log("player death");
}
}
}
// :::::子弹脚本:::::
using UnityEngine;
public class bullet : MonoBehaviour
{
private void OnCollisionEnter(Collision collision)
{
if(collision.collider.tag == "Player")
{
collision.gameObject.GetComponent<health>().takeDamage(10);
// OR
GameObject Em = collision.gameObject;
health healths = Em.GetComponent<health>();
if (healths != null)
{
healths.takeDamage(10);
}
// OR
GameObject clone = collision.collider.gameObject;
health myComponent = clone.GetComponent<health>();
myComponent.takeDamage(10);
}
Destroy(gameObject);
}
}