-2
\$\begingroup\$

In the code, everything is working smoothly, only problem I am facing is that the Destroy(fuelprefabInstance) is not getting Destroyed in OnTriggerEnter Method though the print statements are clearly executed and Destroy in Spawn method works fine as well. What am I missing?

Edit: It infact gives NullReferenceException if I try to set fuelprefabInstance to false instead of destroying the prefab using fuelprefabintsnace.setActive(false);

  public IEnumerator Spawn()
    {
        time1 = Random.Range (1, 5);
        spawning = true;
        timer = 0;
        initial = new Vector3 (Random.Range(rmin, rmax), 0.037f, Random.Range((-7.608864f + 10f),(r.renderer.bounds.max.z + 20)));
        fuelprefabInstance = (GameObject)Instantiate (fuelprefab, initial, transform.rotation);
        yield return new WaitForSeconds (time1);
        spawning = false;
        Destroy (fuelprefabInstance, time1);  //this destroy works
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Player") 
        {
            print("called");
           Destroy (fuelprefabInstance);     //this Doesnot
            print ("destroyed");
                }
    }
\$\endgroup\$
  • \$\begingroup\$ why downvote :/ \$\endgroup\$ – Simran kaur Sep 3 '14 at 22:51
  • \$\begingroup\$ Simran, this sounds like more of a debugging question. Have you attached the debugger and checked the state of fuleprefabInstance? Are you familiar with how to use the debugger? \$\endgroup\$ – MichaelHouse Sep 3 '14 at 23:29
  • \$\begingroup\$ @Byte56: No, I am not really familiar with using Debugger, would be glad to get some reference to that as well or anything that can help regarding the issue.As of now, It appears that fuelprefabinstance is null inside of OnTriggerEnter but nt able to figure out the reason \$\endgroup\$ – Simran kaur Sep 3 '14 at 23:34
  • \$\begingroup\$ Likely because you're destroying it already at the end of the Spawn() method. \$\endgroup\$ – MichaelHouse Sep 3 '14 at 23:47
  • \$\begingroup\$ @Byte56:Not really, because it would not be visible if it was destroyed before, It becomes invisible when it's destroyed and if the OnTriggerEvent is being triggered, I suppose that is not the possibility. \$\endgroup\$ – Simran kaur Sep 5 '14 at 1:26
0
\$\begingroup\$

Comment out the destroy function while you spawn and give it a try. Most likely the object gets destroyed before it even collides with the player. The prefab becomes null and throws the exception.

\$\endgroup\$
  • \$\begingroup\$ I tried doing that and it still did not work. \$\endgroup\$ – Simran kaur Sep 7 '14 at 0:01
  • \$\begingroup\$ Is it getting instantiated properly ? \$\endgroup\$ – Varun Khatri Sep 8 '14 at 8:41
  • \$\begingroup\$ Yes, it does.... \$\endgroup\$ – Simran kaur Sep 9 '14 at 11:24
  • \$\begingroup\$ If you are going to reuse the prefab then you can disable it instead of destroying it. This will help save memory. \$\endgroup\$ – Varun Khatri Sep 9 '14 at 11:52
  • \$\begingroup\$ I have tried doing setActive to false but it gives nullreferenceexception for fuelprefabInstance \$\endgroup\$ – Simran kaur Sep 10 '14 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.