AH!
SO i'm guessing this script is on the main boat that DOES NOT get destroyed? Right? I couldn't find in your posts the relation between the objects but I'm guessing the reason you use references like this is because you are spawning a new boat from the boat you control and that spawned boat is the one you want to destroy.
OK. Wrong way to do it but OK. We will call boat A the controlled boat and boat B the destroyed boat.
First order of business - why? If it gets spawned the moment you destroy it - why bother destroying it? I understand its for a puzzle but if it never looks like it goes anywhere - why bother?
if you REALLY must be destroying and spawning the object i would suggest moving the destroy and instantiate to a spawn class and not on any object that gets destroyed.
If you REALLY want to destroy and instantiate without creating another class then move those functions to object B
Now - TAG object A with anything - for this i will say "ObjectA"
Object B Script:
Code:
void OnTriggerEnter (Collider collision)
{
if (collision.gameObject.CompareTag("ObjectA"))
{
Instantiate (gameObject, transform.Position, transform.Rotation);
Destroy (gameObject);
}
}
That is all you need. No start, no update, no IFs, no references, just an OnTriggerEnter and your class information
All we are doing is checking to see if we collided with ObjectA - if we did - we spawn a clone of ourselves and destroy the current object.
Again - I question this method of instantly destroying and spawning the exact same object in the exact same place but it's not my game.
You really should be using a SpawnController class to hold information on what B objects need to spawn where and create public methods object A can call to spawn B objects.
But if you want to go this method without a controller script on another object then just do it this way. You may just be spawning over and over again if object A is in the same position object B spawns in as every time it spawns OnTriggerEnter may get called because of a constant overlap.
I would personally create a SpawnController which contains the object, transforms, etc that need to be spawned. On this class create a public function to spawn the object at your desired location.
On object B - reference that class so you have access to it.
When object A enters object B, you will turn off the object's renderer and that's it.
Now, when object A leaves object B (OnTriggerExit) - we will call the public function on our SpawnController class to spawn a new B object and immediately after, destroy ourselves since we are of no further use.
This eliminates possible overlap and waits for the previous object to leave the trigger before spawning a new one, eliminating spawn-contact-instantiating.
You can take this a step further by using that spawn method on your SpawnController to call a coroutine and "wait" a moment or two before spawning a new object.
We don't want to call the coroutine from the object we destroy because whatever object starts a coroutine - must be alive to continue the coroutine. A solution would look like:
Code:
public void SpawnNewThing()
{
StartCoroutine("NewObjectThing");
}
IEnumerator NewObjectThing()
{
yield return new waitForSeconds(1); //waits one second or however long you like
Instantiate (blah blah...
}
This will allow you to start a coroutine on an object that you are about to destroy by calling a public function first. That function or method then calls the coroutine and the previous object can safely be destroyed while the coroutine runs.