- Home /
Third person controller not being destroyed although script works
Hello,
In my game, when a third person controller walks into a box collider - I want to destroy that controller and instantiate a new one.
This is the script I wrote to destroy the first controller:
var thirdPerson : GameObject;
function OnTriggerEnter(other: Collider){
if (other.tag == "Player")
{
Destroy(thirdPerson);
}
}
And this is the script to instantiate the new one:
var prefab : Transform;
private var hasPlayed = false;
function OnTriggerEnter () {
var pos : Vector3;
if (!hasPlayed){
Instantiate (prefab);
hasPlayed = true;
}
}
I tested the scripts on two character controllers that I downloaded from the asset store already animated, and they both worked.
However - I've just learnt to animate characters myself (I'm very new to unity) and want to use these instead - but although the new controller is intantiated, the first one isn't destroyed.
I've been through everything I can think of - but I just can't work out why the characters I've animated myself aren't destroyed when the script itself works.
If anyone has any idea why - please help!
Thanks, Laurien
I forgot to add - all of my scripts are in javascript, but the script I'm using to animate my characters is in C Sharp. (as it was the only tutorial I could find that worked)
Is this the problem do you think? (and if so - how do I go about solving it?)
Try comparing the ones that you animated and the ones you didn't.
If one of them worked and one didn't, there must be a difference.
The only difference I can find is that the script I used to animate my characters is in C Sharp, unlike the ones I didn't animate, which use javascript.
But I can't work out if this means that I need to need to make changes to the script in C Sharp, or I need to write a new script in javascript?
Try looking at both scripts and see if there are any major differences.
Did you try looking at the log?
It might tell you why it's not destroying.
Answer by Safforn · May 16, 2013 at 03:50 PM
well if the script is on the character cotroller, it destroys the script and therefore can't execute it anymore. Try putting the script on an empty gameobject, and on the collider just a trigger with a "sendmessage".
The script is on the box collider attached to an empty game object, not the character - so it isn't destroyed - so that shouldn't make a difference?
Your answer
![](https://koobas.hobune.stream/wayback/20220613102354im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Destroy character controller and instantiate new one 1 Answer
Destroying all enemy characters after the level is over 4 Answers
Destroy GameObject With Collision Using C# 1 Answer
Find gameobjects by tag, and destroy if in the distance of a certain game object... 1 Answer