- Home /
Set gameobject components to true or false
I am making my first game, a low graphics flight simulator and I have found a way to use a hinge joint motor to give the illusion. I want it so when you press G, the wheels go in. I have a very scant ammount of coding knowledge with Unity, so I can not do it by myself. Anyhoo, I need nothing simple. Just a script that causes areas you can check off to be unchecked/checked.
Answer by Statement · Dec 10, 2011 at 01:28 AM
I wasn't really sure what you wanted. You just want to toggle some variable true/false or a component enabled/disabled? Well, I provide solutions to both.
If you want to toggle flags on/off with buttons:
var wheels : boolean;
var lights : boolean;
// Keep adding more flags if you want...
function Update() {
if (Input.GetKeyDown(KeyCode.G)) wheels = !wheels;
if (Input.GetKeyDown(KeyCode.L)) lights = !lights;
// Keep adding more toggles if you want...
}
If you want to toggle behaviours on/off with buttons:
// Drag your components to these variables
// in the editor inspector.
var wheels : MonoBehaviour;
var lights : MonoBehaviour;
// Keep adding more components if you want...
function Update() {
if (Input.GetKeyDown(KeyCode.G)) Toggle(wheels);
if (Input.GetKeyDown(KeyCode.L)) Toggle(lights);
// Keep adding more toggles if you want...
}
function Toggle(component : MonoBehaviour) {
if (component)
component.enabled = !component.enabled;
else
print("Missing a component!");
}
Answer by Shrimpalimpa · Dec 11, 2011 at 01:42 AM
What I meant was turning on and off a motor in a hinge joint, but I found a way around that. Thanks anyway.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Simple AI Script help 2 Answers
Overlap Detection HELP!! 0 Answers
Implementing wander mode 0 Answers
this script in Javascript?? 0 Answers