2016-06-18 1 views
0

오브젝트가 파손되었을 때 다른 스크립트 메소드 (별)를 호출하려고합니다. 아래 코드는 지금까지 수행 한 작업입니다. "tim.stars"라인에서 오류 (Null 참조)가 발생했습니다. , 내가 뭘 잘못했는지 어떤 제안? 여기 내 코드가있다.다른 스크립트에서 호출하는 메소드 C# Unity3d

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

동일한 게임 객체에 'TimingForIndust2' 스크립트가 첨부되어 있습니까 ('clear')? 또는'TimingForIndust2'가 충돌을 감지 할'fire1' 태그를 사용하여 gameobject에 첨부되어 있습니까? – Programmer

+0

clear 스크립트에는 my (소화기 prefeb)가 인벤토리에서로드되고 TimingForIndust2 스크립트가 Timer에 연결됩니다 (타이머는 실제로 시간이 실행되는 캔버스 임). 클리어 스크립트 및 TimingForIndust2 스크립트가 다른 개체에 연결되어 있습니다. @Programmer –

답변

1

제 2 스크립트입니다, 당신은 GameObject.FindTimer 다음 그것에서 구성 요소를 얻을 찾을 수있다. 당신은 충돌이있을 때마다 이것을 할 수 없습니다. 다시 한 번 Start() 함수에 캐시해야합니다.

또한 새 코드에서는 ParticleSystem을 캐싱했습니다. 또한 태그를 obj.gameObject.tag == "fire1"과 직접 비교하는 대신 CompareTag 함수를 사용하여 태그를 비교하십시오.

이렇게하면 문제가 해결됩니다. 이제 TimingForIndust2 스크립트에서 updat 함수로 호출하는 TextIndicator 스크립트에 첨부 된 Text 구성 요소를 캐시하는 것이 좋습니다.

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

답변 주셔서 감사합니다.하지만 답변을 올리면이 오류가 표시됩니다. ArgumentException : GetLocalizedString은 주 스레드에서만 호출 할 수 있습니다. 장면을로드 할 때 생성자 및 필드 초기화 프로그램이로드 스레드에서 실행됩니다. 생성자 또는 필드 초기화 프로그램에서이 함수를 사용하지 말고 초기화 코드를 Awake 또는 Start 함수로 이동하십시오. TreeEditor.TreeGroupLeaf..cctor() TypeInitializationException으로 다시 throw합니다 : TreeEditor.TreeGroupLeaf에 대한 형식 이니셜 라이저에 의해 예외가 throw되었습니다. –

+0

@FarhanAli 내가 제공 한 대답은 다른 스크립트에서 다른 함수를 호출하는 것입니다. 그것은 또한 당신의'null' 문제를 해결해야합니다. 이 질문에 답을 얻었습니다. 새로운 오류는 완전히 다른 것입니다. 이 질문을 닫고 새 질문을 올릴 수 있습니다. 또한 문제가 발생하는 코드 줄을 제공하고 스크립트를 게시하지 않으면 해당 오류를 복제하는 것이 거의 불가능하기 때문에 모든 스크립트를 게시하십시오. 원하는 경우 여기에 새 질문을 연결할 수 있습니다. – Programmer

+0

정확하게 고마워요, 알아 냈습니다 :-) –

관련 문제