从另一个脚本访问变量/函数所以我试图通过触摸一个立方体来改变另一个脚本中的变量。电流设置1x播放器1倍敌人每个人都有自己的脚本Enemy_Stats & Character_Stats正如您在这个小片段中所看到的,从另一个脚本访问变量是一个很好的解决方法。void OnCollisionEnter(Collision collision){
if (collision.gameObject.tag == "Enemy")
{
collision.gameObject.GetComponent<Enemy_Stats>().Health =
collision.gameObject.GetComponent<Enemy_Stats>().Health
- gameObject.GetComponent<Character_Stats>().AttackDamage;
if (collision.gameObject.GetComponent<Enemy_Stats>().Health <= 0)
{
Destroy(collision.gameObject);
}
}}我是统一的新手,但是难道没有一种方法可以用这样的东西来引用它:collision.Health?
3 回答
沧海一幻觉
TA贡献1824条经验 获得超5个赞
public
private
.
public class ScriptA : MonoBehaviour{ public int playerScore = 0; void Start() { } public void doSomething() { }}
playerScore
ScriptA
ScriptB
GameObject.Find
GetComponent
public class ScriptB : MonoBehaviour{ ScriptA scriptInstance = null; void Start() { GameObject tempObj = GameObject.Find("NameOfGameObjectScriptAIsAttachedTo"); scriptInstance = tempObj.GetComponent<ScriptA>(); //Access playerScore variable from ScriptA scriptInstance.playerScore = 5; //Call doSomething() function from ScriptA scriptInstance.doSomething(); }}
牧羊人nacy
TA贡献1862条经验 获得超7个赞
Health
Enemy_Stats
Component
Player_Stats
Start
Destroy
Enemy_Stats
Cats萌萌
TA贡献1805条经验 获得超9个赞
gameObject.GetComponent<Character_Stats>()
Start()
GetComponent
Dictionary<GameObject, Enemy_Stats>
- 3 回答
- 0 关注
- 518 浏览
添加回答
举报
0/150
提交
取消