Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by FirstLaw · Mar 20, 2014 at 05:28 PM · prefabs

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FirstLaw · Mar 20, 2014 at 08:08 PM 0
Share

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?

avatar image FirstLaw · Mar 20, 2014 at 08:19 PM 0
Share

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!!!!!

avatar image LMan · Mar 20, 2014 at 08:35 PM 0
Share

haha excellent! You are welcome- glad I could help!

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FirstLaw · Mar 20, 2014 at 08:10 PM 0
Share

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)

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FirstLaw · Mar 20, 2014 at 08:11 PM 0
Share

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!

avatar image FirstLaw · Mar 20, 2014 at 08:43 PM 0
Share

okay now i understand what you mean... thank you too you also had the answer i was looking for ! Cheers!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges