- Home /
how to disable a another script after it runs.
i have a script that disables another scripts when i press a button that has "transform.position = Vector3.MoveTowards ..." and activates another objects scrpitc. Activating and disaclebing works fine but ater disable this scritp that has "transform.position = Vector3.MoveTowards..." line the object it moves stop becousr script is disabled. How can i disable that script after it finishes its moves.?
We need to see your scripts. One way to accomplish this task is to tell the other script to disable itself when it reaches its destination, but the how will depend on your scripts.
Answer by Uncasid · Jan 20, 2014 at 04:36 PM
You can use events to trigger things, then just listen for those events in another class, or the same class:
System.Action SomethingHappened;
if (SomethingHappened != null) { SomethingHappened(); }
You would put this at the end of your logic that determines when the object has actually finished its objective. You will need to do a check to see that the object is at the position you want, don't do a position == vector check, it won't work. Do a distance check with a range that you consider ok.
I realized I should add a little more to help you out with this (put something like this at the end of your update):
public System.Action FinishedMovement;
float threshold = 0.1f;
void Update() {
// Blah blah tranform.position.MoveToward(blahblah)
if (Vector3.Distance(targetVector, transform.position) <= threshold) {
if (FinishedMovement != null) { FinishedMovement(); }
}
}
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Fade an object in and out 0 Answers
(C#)How To Disable Gravity From Script? 4 Answers
how to destroy an object permanently ? 1 Answer
Disable shooting whilst reloading? 1 Answer