0

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? 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

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);
    }
}
}
2
  • Is TimingForIndust2 script attached to the-same gameobject the script above (clear) is attached to? Or is TimingForIndust2 attached to the gameobject with the fire1 tag that you will detect the collision to? Commented Jun 18, 2016 at 3:54
  • clear script is attached with my (fire extinguisher prefeb which load from inventory), and TimingForIndust2 script is attached to Timer(timer is actually canvas where time is running). clear script and TimingForIndust2 script are attached at different objects. @Programmer Commented Jun 18, 2016 at 4:12

1 Answer 1

1

By reading your comment, you have to Find the Timer with GameObject.Find then get the component from it. 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.

I also cached ParticleSystem in the new code. Also, instead of comparing tag directly with obj.gameObject.tag == "fire1", use the CompareTag function to compare tags.

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.

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);
}
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for answer, but now I am getting this error, by placing your answer ArgumentException: GetLocalizedString can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function. TreeEditor.TreeGroupLeaf..cctor () Rethrow as TypeInitializationException: An exception was thrown by the type initializer for TreeEditor.TreeGroupLeaf
@FarhanAli The answer I provided should do what you are looking for which is to call another function in another script. It should also solve your null problem. This question has been answered. The new error you are getting is something totally different. You can close this question and post a new question. Also provide the line of code that causes the problem and post all your scripts because without it, replicating that error would be almost impossible. You can link the new question here if you want.
exactly, Thank You much, I have figured out :-)
@FarhanAli That's fine. Happy coding!
One thing more, timingForIndust2.stars (); method is calling again and again the function at each hit, can here we do such thing, it calls only once when object is completly destroyed, any check?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.