- Home /
How to make trigger removes script.
Hi,i want script removes when player is in trigger,but how? Here is my script,its unfinished,i dont know what to do next,so please help. :)
function OnTriggerEnter (other : Collider) {
if (other.tag == "Player"){
GameObject.Find ("Wall").GetComponent("FixedJoint");
}
}
Answer by Berenger · Jan 23, 2013 at 03:25 PM
I don't know what you want to remove, but you need to use the Destroy function, either on a component or a game object.
You don't have to SHOUT, be nice and nice things will happen. Check my answer here for enabling or disabling scripts : http://answers.unity3d.com/questions/375514/switch-scripts.html
Im not shouting :D but i want to destroy script not disable it,but destroy it :)
Well my reply is exactly the same as $$anonymous$$aster Berenger : I don't know what you want to remove, but you need to use the Destroy function, either on a component or a game object
What script are you talking about? On what object? There is no RemoveComponent command anywhere I looked. How is destroying a script going to be different from enabling it? From my reading this 'site, a good practice of game design is to not use Instantiate or Destroy unless absolutely completely necessary, rather use pooling techniques with enable and/or SetActiveRecursively.
Edit :
you were advised about not using CAPS on this question : http://answers.unity3d.com/questions/376840/action-activates-trigger.html
also every question you asked without an accepted answer is a request to write code for you.
I cannot believe I am doing this, sorry for taking this on after you Berenger (upvoted). Untested code, for learning purposes only, use at your own risk.
var wallScript : TheNameOfTheScriptOnTheWallHere; // typecast variable to the name of the script
function Start()
{
// Use GetComponent at Start wherever possible
var wallObject : GameObject = GameObject.Find("Wall"); // find the wall object
wallScript = wallObject.GetComponent( TheNameOfTheScriptOnTheWallHere ); // find the script component on the wall object
}
function OnTriggerEnter (other : Collider)
{
if (other.tag == "Player")
{
Destroy( wallScript ); // say bye-bye to the script
}
}
Unity Scripting Reference : http://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html
Just a couple of links by searching (google is your friend, simply search the title to your question) :
http://answers.unity3d.com/questions/223276/object-destroys-on-collision-script.html
http://answers.unity3d.com/questions/32446/collision-script-to-destroy.html
Thanks man,you are the BEST,FOR REAL,you should use your skills to make something with unity...