- Home /
Deactivate Controlle of one object and Contoll another
I have a space ship you can fly around and do all sorts of cool things with but I want to make it so when it get in range or Collides with a trigger of the big ship it switches the controlls over to it. I plan to have the big ship just siting out in space so i can have all my scripts on it and they will just be turned off. I am just unsure how to go about turning a script on/off during a collison.
Answer by sparkzbarca · Oct 12, 2012 at 12:15 AM
also assuming its a collision the function your looking for is.
void OnCollisionEnter() as documented at http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html
alternetly if you want it to be a trigger its
void OnTriggerEnter()
simply add a blank script to the object that will be hit. for example
//destroy thing that hit me
void OnTriggerEnter(collider thing_that_hit_me)
{
destroy(thing_that_hit_me.GameObject);
}
//disable the controls for what hit me and enable mine
void OnTriggerEnter(collider what_hit_me)
{
//enable the big ships controls assuming the big ship is
// what this script is attached to
GetComponent<ship_controls>.enable = true;
//disable the ship that hit me.
what_hit_me.GetComponent<ship_controls>.enable = false;
}
hope that helps
Answer by kmeboe · Oct 11, 2012 at 11:07 PM
You can get scripts using GetComponent, like so (c#):
MyScript script = myShip.GetComponent() as MyScript;
Once you have the script, you can turn it off with .enabled:
script.enabled = false;
If you have a lot of scripts to turn on and off, it might be nice to have one script (per ship) that has knowledge of all of the other scripts, and has a method that will turn them on or off. The signature would be something like this:
void EnableScripts (bool enable);
Good luck!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Space Craft controller. 1 Answer
PinBall Flippers 1 Answer
How to use Android Dual Joysticks? 1 Answer