从另一个脚本访问变量/函数所以我试图通过触摸一个立方体来改变另一个脚本中的变量。电流设置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贡献2003条经验 获得超2个赞
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(); }}
桃花长相依
TA贡献1860条经验 获得超8个赞
Health
Enemy_Stats
Component
Player_Stats
Start
Destroy
Enemy_Stats
翻翻过去那场雪
TA贡献2065条经验 获得超13个赞
gameObject.GetComponent<Character_Stats>()
Start()
GetComponent
Dictionary<GameObject, Enemy_Stats>
添加回答
举报
0/150
提交
取消