从另一个脚本 C# Unity3d 调用方法
call method from another script C# Unity3d
我想在对象被销毁时调用另一个脚本方法 (stars),下面是我到目前为止完成的代码,我在第 "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);
}
}
}
通过阅读您的评论,您必须找到 Timer
和 GameObject.Find
然后从中获取组件。每次发生碰撞时,您都不能这样做。您必须将其缓存在 Start()
函数中一次,然后再使用它。
我还在新代码中缓存了ParticleSystem
。此外,不要直接与 obj.gameObject.tag == "fire1"
比较标签,而是使用 CompareTag
函数来比较标签。
这应该可以解决您的问题。现在,您的工作是缓存附加到 TextIndicator
脚本的 Text
组件,您在 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);
}
}
我想在对象被销毁时调用另一个脚本方法 (stars),下面是我到目前为止完成的代码,我在第 "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);
}
}
}
通过阅读您的评论,您必须找到 Timer
和 GameObject.Find
然后从中获取组件。每次发生碰撞时,您都不能这样做。您必须将其缓存在 Start()
函数中一次,然后再使用它。
我还在新代码中缓存了ParticleSystem
。此外,不要直接与 obj.gameObject.tag == "fire1"
比较标签,而是使用 CompareTag
函数来比较标签。
这应该可以解决您的问题。现在,您的工作是缓存附加到 TextIndicator
脚本的 Text
组件,您在 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);
}
}