Destroy (gameobject) doesn't work during build run/export

Like the title says, my game is not working correctly when I build and run it. I have a simple script that says

function OnTriggerEnter (collision : Collider)
{
    Network.Destroy (gameObject);
    Debug.Log ("gameObject should be destroyed");
}

which works perfectly fine when played during editor preview. I have several of these scripts for different prefabricated objects and none of them work during a build run. My game is setup for network play and I had a look around the internet for an answer but mainly what turned up are physics related problems with the car tutorial. Admittedly our physics does act different during build run but it's alot smoother and more dynamic (which is a good thing is this case) than in editor mode. I'm not sure how relevant that is but it may help to give you a bigger picture of my situation. My question is has anyone else experienced problems such as this and if so what steps did you take to solve it? Should I try a different method to calling destroy (gameObject)? Is there particular export settings which I should adjust? Any help people may provide will be greatly appreciated.

UPDATE

As per Keith's post I have added this line of code to the script

Network.Destroy (GetComponent(NetworkView).viewID);

and its returned two errors

View ID AllocatedID: 0 not found during lookup. Strange behaviour may occur

and

Couldn't destroy object because the associate network view was not found

of which when googled I found one result which turned out to be caused by an object being inactive, mine are all active however. Got to love Unity's infinite usefulness in saying "Strange behavious may occur" as though my computer is going to flip out and start thinking it's a toaster or something lol.

May be a stupid question, but do you get the same result passing the NetworkView.viewID of the object into Network.Destroy?

It's not a stupid question, you made me realise that there wasn't a networkview component on them #facepalm# I've added a network view and it still doesn't work. So I'm trying to do what you have said but I may not be doing it right. Will update the post

2 Answers

2

Okay dokay have this one fixed now sometimes you just have to bash your head against a problem till your head hurts or it goes away. I had some other code in my Ontrigger function so it looked like this

function OnTriggerEnter (collision : Collider)
{
    if (collision.gameObject.CompareTag  ("player1")) 
    {
        FlameOn1 = playerPrefab1.GetComponent(PlayerMove1);
        FlameOn1.FTBOOL = true;
        audio.PlayOneShot(flamepickupnoise);
    }
    Network.Destroy (gameObject)
    Debug.Log ("gameObject should be destroyed");
}

and the debug log was posting fine and it worked in editor mode so i assumed that the script was fine. However in order to get it to work in build mode i hade to move one line of code

function OnTriggerEnter (collision : Collider)
{
    Network.Destroy (gameObject);
    if (collision.gameObject.CompareTag  ("player1")) 
    {
        FlameOn1 = playerPrefab1.GetComponent(PlayerMove1);
        FlameOn1.FTBOOL = true;
        audio.PlayOneShot(flamepickupnoise);
    }
    Debug.Log ("gameObject should be destroyed");
}

This has fixed it but I cannot figure out why, for all the playboy bunnies in the world, this worked? Is this a bug or have I missed an essential concept to scripting that this applies to? If anyone could clear up why it has acted this way I would be able to sleep better at night :)

I have tried this on my side just using my player object quickly. I added a Network View to it, and inside it's OnCollisionEnter used this line, and it works in the editor and in build.

void OnCollisionEnter(Collision TheCollision)
{
    Network.Destroy(networkView.viewID);
}

Strangely, using OnTriggerEnter on the other hand, I get the exact errors you mentioned above.

EDIT: Answer related to using OnTriggerEnter.

If the object you're colliding with perhaps has children who are triggers or collidable, it will cause the OnTriggerEnter on your object to fire again for each collision.

EDIT: Updated this code below. I've noticed though that the destroy deactivates the object and this is far more reliable and elegant :)

void OnTriggerEnter(Collider TheCollider)
{
    if (this.enabled == true)
    {
        Network.Destroy(networkView.viewID);
    }
}

Sorry for this being in C#, I'm sure you can port it over to JS simply enough though.

Of course, OnCollisionEnter will only work if your collider isn't marked as a Trigger. So this may not be a solution for you at all.

nope sorry to say did everything like you said and that doesn't work either. I just collide with the objects now and they don't destroy. It was a useful answer though, I may need this later on but thats a different subject and I shouldn't tangent from this post.

Ah no problem. I think I know what's going on, the object you're colliding with.. Is it also a trigger?

it wasn't a trigger when I tried this approach but it is when I have onTriggerEnter in the script. Don't want to make another school boy error lol

Lol. Well if you want to use OnTriggerEnter in your script, I'm pretty sure that my edited answer will give you the desired result. :)