簡體   English   中英

腳本連接到其他腳本

[英]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() Function。

如果是這樣,您可以將moveSpeed變量標記為static並且您可以訪問它。 我認為 class 中的這個變量繼承自 monobehaviour。 如果您想要所有公共變量,即使不是 static 也可以使用singleton 模式 Singleton 模式在很多情況下都非常有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM