- Home /
gameObject A detecting keyboard inputs from gameObject B.
Hi! I'm very new with unity. Been self studying the program for a week already.
I have
gameObject A
function Update () {
if(Input.GetButton("Fire1")){
renderer.enabled = true;
}
else {
renderer.enabled = false;
}
}
gameObject B
//I want to put a code here which will make gameObject B do something whenever it detects gameObject A inputting keyboard press such as Fire1. Thanks in advance!
Answer by robertbu · Mar 17, 2014 at 04:46 AM
Input.GetButton*() functions are not object specific. You can detect that specific button in any number of script. BTW, you probably what Input.GetButtonDown() in your code above. GetButtonDown() returns true for the single frame the button goes down. GetButton() returns true all the time the button is held donw.
While @robertbu is 100% correct, you also might not want to start putting that repeated do-stuff-on-click in every script, so to answer the other part of your question, you should look at examples on GetComponent and ways to pass actions/parameter/call functions, etc from script A to B (on a diff GameObject)
Your answer
Follow this Question
Related Questions
GetKeyDown code turned into a int issue [SOLVED] 1 Answer
Checking whether string is a valid Input.Key 1 Answer
setting iPhoneKeyboard.text (or any workaround about resetting user input) in Android. 0 Answers
Android Keyboard .text String returning empty? 1 Answer
Press GUI button with key press 1 Answer