簡體   English   中英

來自另一個腳本C#Unity3d的調用方法

[英]call method from another script C# Unity3d

我想在銷毀對象時調用另一個腳本方法(星號),以下是我到目前為止已完成的代碼,在“ tim.stars”行出現錯誤(空引用),有什么建議我做錯了嗎? 這是我的代碼。

using UnityEngine;
using System.Collections;
public class clear : MonoBehaviour {
// Use this for initialization
void Start () {
    GetComponent<ParticleSystem> ().emissionRate = 0;
}
// Update is called once per frame
void Update () {
    if (Input.GetMouseButtonDown (1)) {
        GetComponent<ParticleSystem> ().Emit (10);
    }
}
void OnParticleCollision(GameObject obj)
{
    if (obj.gameObject.tag == "fire1") {
        Destroy (obj, 5.0f);
        TimingForIndust2 tim = GetComponent<TimingForIndust2> ();
        tim.stars ();
    }
        StartCoroutine (TestCoroutine());
    }
IEnumerator TestCoroutine(){
    yield return new WaitForSeconds(8);
    Application.LoadLevel (25);
}
}

這是我的第二個腳本TimingForIndust2

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using MadLevelManager;
public class TimingForIndust2 : MonoBehaviour {
public Transform TimingBar;
public Transform TextIndicator;
public Transform TextRemaining;
[SerializeField] private float currentAmount;
[SerializeField] private float speed;

// method to reduce the time continously
void Update () {

    if (currentAmount > 0) {
        currentAmount -= speed*Time.deltaTime;
        TextIndicator.GetComponent<Text>().text=((int)currentAmount).ToString()+"s";
        TextRemaining.gameObject.SetActive(true);

    } else {
        TextRemaining.gameObject.SetActive(false);
        TextIndicator.GetComponent<Text>().text="TimeUP";
        Application.LoadLevel (62);

    }
    TimingBar.GetComponent<Image> ().fillAmount = currentAmount / 60;
}
public void stars()
{
    if (currentAmount > 45.0f) {

        MadLevelProfile.SetLevelBoolean (MadLevel.currentLevelName, "star_1", true);
        MadLevelProfile.SetLevelBoolean (MadLevel.currentLevelName, "star_2", true);
        MadLevelProfile.SetCompleted (MadLevel.currentLevelName, true);
    } else if (currentAmount > 20.0f && currentAmount < 29.0f) {

        MadLevelProfile.SetLevelBoolean (MadLevel.currentLevelName, "star_1", true);
        MadLevelProfile.SetLevelBoolean (MadLevel.currentLevelName, "star_2", true);
        MadLevelProfile.SetCompleted (MadLevel.currentLevelName, true);

    } else if (currentAmount > 2.0f && currentAmount < 19.0f) {

        MadLevelProfile.SetLevelBoolean (MadLevel.currentLevelName, "star_1", true);
    }
}
}

通過閱讀注釋,您必須找到帶有GameObject.FindTimer ,然后從中獲取組件。 您不能每次都發生碰撞時執行此操作。 您必須將其緩存在Start()函數中一次,然后重新使用它。

我還在新代碼中緩存了ParticleSystem 此外,不要使用obj.gameObject.tag == "fire1"直接比較標簽,而應使用CompareTag函數比較標簽。

這應該可以解決您的問題。 現在,您的工作是緩存附加到TextIndicator腳本的Text組件,該腳本正在TimingForIndust2腳本中的TimingForIndust2函數中調用。

using UnityEngine;
using System.Collections;
public class clear : MonoBehaviour {
TimingForIndust2 timingForIndust2;

ParticleSystem particles;

// Use this for initialization
void Start () {
    particles = GetComponent<ParticleSystem> ();
    particles.emissionRate = 0;

    GameObject tempObj = GameObject.Find("Timer");
    timingForIndust2 = tempObj.GetComponent<TimingForIndust2>();
}
// Update is called once per frame
void Update () {
    if (Input.GetMouseButtonDown (1)) {
        particles.Emit (10);
    }
}
void OnParticleCollision(GameObject obj)
{
    if (obj.CompareTag("fire1")) {
        Destroy (obj, 5.0f);
        timingForIndust2.stars ();
    }
        StartCoroutine (TestCoroutine());
    }
IEnumerator TestCoroutine(){
    yield return new WaitForSeconds(8);
    Application.LoadLevel (25);
}
}

暫無
暫無

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

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