- Home /
How can i bind Prefab for a Keyinput?
Hello , I'm trying to make a typing game ( like guitar hero but with letter from the keyboard) I've made sprites representing each Letter ( say A has a sprite A ) i made a spawner and for each letter i made a fly script that makes the Letter fly towards our hero. Now i put an aiming point to which when the Prefab( A ) hits the Aim and you press A on the keyboard . the Prefab gets destroyed and gives me a score... here is my script
public class fly : MonoBehaviour {
score sc;
public Rigidbody2D flyingobj;
public float speed = -7f;
// Use this for initialization
void OnTriggerStay2D(Collider2D other)
{
if (other.tag == "aim" && Input.GetKeyDown(KeyCode.A))
{
sc = GameObject.Find("MainCamera").GetComponent<score>();
sc.IncreaseScore(2);
Destroy(this.gameObject);
}
}
void Start ()
{
// Set the prop's velocity to this speed in the x axis.
flyingobj.velocity = new Vector2(speed, 0);
}
// Update is called once per frame
void Update () {
}
}
okay now the problem is that I have 26 letters ... that means i have to make a script for each letter ... is there a way to make a generic script and then for each prefab bind it to a key input? like say
{
if (other.tag == "aim" && Input.GetKeyDown(KeyCode.(generic)))
}
and generic is A in the A prefab... [I know that this code is wrong , i'm just trying to explain myself]
Or is there an easier way to do this? Thank you for your time! PS : if you haven't noticed already I'm a beginner in Unity and scripting all together I'm learning as i go
Answer by LMan · Mar 20, 2014 at 05:49 PM
Simplest thing to do might be to make a public variable and use that as the key code. Then go through your prefabs and set the variable through the editor for each letter.
How to make the public variable?! i tried public float kk = A; ~~~~ void OnTriggerStay2D(Collider2D other) { if (other.tag == "aim" && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.kk))
But it doesn't work... how would i be able to set a public variable?
Okay i got it working! I did Public $$anonymous$$eyCode kk= $$anonymous$$eyCode.A; and then Get$$anonymous$$eyDown(kk) Fixed it ! THAN$$anonymous$$ YOU!!!!!
Answer by sriram90 · Mar 20, 2014 at 06:14 PM
Assuming you're trying it with KeyBoard Inputs,
Public bool triggeredWithAim = false;
void Update()
{
if(Input.GetKey(KeyCode.A))
{
if(triggeredWithAim)
{
//Key Board Pressing A.
//Destroy This (A) object.
}
}
}
Void OnTriggerEnter(Collider obj)
{
if(obj.gameObject.tag == "aim")
{
//triggered with aim
this.triggeredWithAim = true;
}
}
Better you may save all keycodes in a list then use switch case on Update.
your way is nice ... but i would still have to make a different script for each prefab... unless I'm missing something ( forgive my noobness)
Answer by Arshia001 · Mar 20, 2014 at 06:47 PM
Do this:
class Whatever { public KeyCode key;
void SomeFunction() { if (Input.GetKeyDown(key)) DoSomething(); } }
Enums are data types, and can be declared publicly and assigned values in Unity Editor; it's done rather cleverly as the Editor automatically shows you the names of the enum's members instead of their numeric values. They can also be stored in variables and accessed without explicitly specifying their name. Thinking enums (such as KeyCode) can only be accessed by name is a rather common mistake.
I'm not familiar with Enums ... so I'll study these and get back to you to see if that works for what i need :D cheers!
okay now i understand what you mean... thank you too you also had the answer i was looking for ! Cheers!
Your answer

Follow this Question
Related Questions
PreFabs not colliding with other GameObjecs, but collide with themselves. 0 Answers
How to fix the problem of object instantiating partially? 0 Answers
Line from Parent to Children on UI 0 Answers
How would I go about storing spawned prefabs to acces them ? 3 Answers
What files are responsible for script components on prefabs? 2 Answers