简体   繁体   English

来自另一个脚本C#Unity3d的调用方法

[英]call method from another script C# Unity3d

I want to call another script method (stars) when object is destroyed, Below is my code so far I had done, I am getting error (Null reference) at line "tim.stars", Any suggestions what I had done wrong? 我想在销毁对象时调用另一个脚本方法(星号),以下是我到目前为止已完成的代码,在“ tim.stars”行出现错误(空引用),有什么建议我做错了吗? Here is my code. 这是我的代码。

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);
}
}

here is my 2nd script TimingForIndust2 这是我的第二个脚本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);
    }
}
}

By reading your comment, you have to Find the Timer with GameObject.Find then get the component from it. 通过阅读注释,您必须找到带有GameObject.FindTimer ,然后从中获取组件。 You can't be doing this each time there is a collision. 您不能每次都发生碰撞时执行此操作。 You have to cache it in the Start() function once then re-use it. 您必须将其缓存在Start()函数中一次,然后重新使用它。

I also cached ParticleSystem in the new code. 我还在新代码中缓存了ParticleSystem Also, instead of comparing tag directly with obj.gameObject.tag == "fire1" , use the CompareTag function to compare tags. 此外,不要使用obj.gameObject.tag == "fire1"直接比较标签,而应使用CompareTag函数比较标签。

This should fix your problem. 这应该可以解决您的问题。 Now, it's your job to cache the Text component that is attached to the TextIndicator script which you are calling in the updat function from the TimingForIndust2 script. 现在,您的工作是缓存附加到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