- Home /
Activate a Object
I'm working on a top down game with a machine gun, and I was wondering how to make an object/cannon active when I walk to it and press the "F" button. Just like in "Skylanders" for Wii.
Felipe
Answer by Berenger · Jan 28, 2012 at 05:11 PM
I'm not sure what you means by activate. There is an activate boolean on avery GameObject, so you just need to get a reference to it and object.active = false / true, but it doesn't feel like that's what you mean (A deactivated object disable all it's components, thus is invisible).
Do you want to enable an AI script that makes an object perform some action ? Then when you press the F button from another script, do object.GetComponent().enabled = true;
Yes, for what you said there about the AI. So the conclusion is to make a script and attach it to the player that sends a message saying that the Cannon has to fire if I get 0.1 meter close and when I press F, right?
Either on the player, either on the canon itself as you can attach more than one script. The first script would always be enabled, check for the player proximity / keyboard input and enable the second script when the time is right.