脚本连接到其他脚本
Script connection to other Script
我有一个脚本在不同对象中给我的老板。我在 boss 的生命值是 10 或更低时编写脚本,将另一个脚本的速度设置为 4。我将第一个脚本附加到其他脚本但不起作用,当生命值低时速度不会改变并且我不知道是什么问题。
public int health = 12;
private gameMaster gm;
private UnityEngine.Object explosionRef;
public GameObject Hiedra;
public bool isHurt = false;
private HiedraScript ChangeVelocity;
void Start()
{
gm = GameObject.FindGameObjectWithTag("GameMaster").GetComponent<gameMaster>();
explosionRef = Resources.Load("Explosion");
ChangeVelocity = gameObject.GetComponent<HiedraScript>();
}
void Update()
{
}
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Bullet"))
{
Destroy(collision.gameObject);
health--;
Hiedra.GetComponent<Animation>().Play("Player_RedFlash");
if (health <= 10)
MovesFaster();
isHurt = true;
if (health <= 0)
KillSelf();
}
if (health <= 0)
{
gm.points += 20;
}
}
private void MovesFaster()
{
ChangeVelocity.moveSpeed = 4f;
}
和不同脚本中的其他值:
public float moveSpeed = 1f;
你试过 Debug.Log() 了吗?
如果统一记录一些东西,那么你就会知道它是否真的调用了 MovesFaster() 函数。
如果是这样,你可以将moveSpeed变量标记为static,你就可以访问它了。我认为这个变量在 class 中继承自 monobehaviour。如果你想要所有 public 变量,即使不是静态的,你也可以使用 单例模式 。单例模式在很多情况下非常有用。
我有一个脚本在不同对象中给我的老板。我在 boss 的生命值是 10 或更低时编写脚本,将另一个脚本的速度设置为 4。我将第一个脚本附加到其他脚本但不起作用,当生命值低时速度不会改变并且我不知道是什么问题。
public int health = 12;
private gameMaster gm;
private UnityEngine.Object explosionRef;
public GameObject Hiedra;
public bool isHurt = false;
private HiedraScript ChangeVelocity;
void Start()
{
gm = GameObject.FindGameObjectWithTag("GameMaster").GetComponent<gameMaster>();
explosionRef = Resources.Load("Explosion");
ChangeVelocity = gameObject.GetComponent<HiedraScript>();
}
void Update()
{
}
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Bullet"))
{
Destroy(collision.gameObject);
health--;
Hiedra.GetComponent<Animation>().Play("Player_RedFlash");
if (health <= 10)
MovesFaster();
isHurt = true;
if (health <= 0)
KillSelf();
}
if (health <= 0)
{
gm.points += 20;
}
}
private void MovesFaster()
{
ChangeVelocity.moveSpeed = 4f;
}
和不同脚本中的其他值:
public float moveSpeed = 1f;
你试过 Debug.Log() 了吗? 如果统一记录一些东西,那么你就会知道它是否真的调用了 MovesFaster() 函数。
如果是这样,你可以将moveSpeed变量标记为static,你就可以访问它了。我认为这个变量在 class 中继承自 monobehaviour。如果你想要所有 public 变量,即使不是静态的,你也可以使用 单例模式 。单例模式在很多情况下非常有用。