- Home /
destroy hinge joint component from another object in javascript
Hi, you'll see, i have this code, i need to destroy a hinge joint component of a door from the script of a trigger, but when i run the code, it erases the entire door, what am doing wrong or what can i do? i don't want to have a script per door because there are so many of them :I
#pragma strict
var levelStuff : GameObject;
var activar : boolean;
var mustKill : boolean = false;
var doorToKill : GameObject;
var killLevel : GameObject;
var createSomething : boolean = false;
var somethingToCreate : GameObject;
function Start () {
}
function Update () {
}
function OnTriggerEnter(col : Collider)
{
if(col.gameObject.tag == "personaje")
{
if (activar == false)
{
levelStuff.SetActive (false);
Debug.Log("borrado");
}
if (activar == true)
{
levelStuff.SetActive (true);
Debug.Log("dibujado");
}
if (mustKill == true)
{
Debug.Log("puerta cerrada");
Destroy(doorToKill.GetComponent(HingeJoint)); //<- this is the problem, i think :I
Destroy(killLevel);
}
if (createSomething == true)
{
somethingToCreate.SetActive(true);
}
}
}
Thnks :b
Answer by rustofelees · Aug 12, 2015 at 06:26 PM
Just a quick thought, but have you tried commenting out the Destroy(killLevel) line to see if it still destroys the door. Is it possible that your door is a child of what is assigned to killLevel? Also, if this doesn't work, have you tried commenting out both lines to ensure that some other code isn't destroying the door?
yes i tried, it is that piece of code that erases the entire door ins$$anonymous$$d of just deleting the component :I
Just to be clear it is the Destroy(killLevel) that destroys your door? The problem goes away if you comment that line out?
no, it is the Destroy(doorTo$$anonymous$$ill.GetComponent(HingeJoint));
part, if i comment that line out, everything else work just fine, but when i add it back again, it deletes the entire door, not just the hinge joint component :I
I'd keep breaking the code apart into individual pieces. Can you assign doorTo$$anonymous$$ill.GetComponent(HingeJoint) to a variable and print the result to see if it's as you'd expect?
Answer by LudwigVK · Aug 12, 2015 at 07:16 PM
oh, i found out what happens: when i destroy the hinge joint, the door falls down... if i also freeze it's rotation & position it should work as i intend, right? to block the door so you can't enter... Thnks :b
Sounds like you have a rigid body attached to the door. If so, you should be able to mark it as kinematic and it shouldn't move.
Your answer
![](https://koobas.hobune.stream/wayback/20220611235601im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiate prefabs before it comes into view 0 Answers
Can someone explain the destroy () command? 1 Answer
Destroy cube on collision 1 Answer
Projectile Script Works Until Destroying Projectile 0 Answers