Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Compare sprite with Keycode - or something like that

Discussion in 'Scripting' started by Mateusz Gryczan, Nov 22, 2014.

  1. Mateusz Gryczan

    Mateusz Gryczan

    Joined:
    May 10, 2014
    Posts:
    10
    Hi, I have script which random my sprites and sets them on the screen.
    I'm dont have idea, How to check sprite with keycode. I'm trying to do, When player press keycode like "C", script will compare this "C" with my letter C from sprite. Sorry for my poor english, I hope someone will understand.

    Here is main script:
    Code (CSharp):
    1. #pragma strict
    2.  
    3. var countDownScript : countDown;
    4. var sprites : Sprite[];
    5. var currentSprite : int = -1;
    6. var startRandomLetters : boolean = false;
    7.  
    8.  
    9. function Start () {
    10.     countDownScript = GameObject.FindGameObjectWithTag("MainCamera").gameObject.GetComponent(countDown);
    11. }
    12.  
    13. function FixedUpdate() {
    14.     if(countDownScript.timer == 0) {
    15.         startRandomLetters = true;
    16.     }
    17.     if(currentSprite == -1 && startRandomLetters) {
    18.         currentSprite = sprites.Length + 1;
    19.         currentSprite = Random.Range(0, sprites.Length);
    20.         GetComponent(SpriteRenderer).sprite = sprites[currentSprite];
    21.     }
    22. }
    23.  
    Testing:
    Code (CSharp):
    1. #pragma strict
    2.  
    3. var randomSpriteScript : randomSprite;
    4. var InputPress;
    5.  
    6. function Start () {
    7.     randomSpriteScript = GameObject.FindGameObjectWithTag("Letter").gameObject.GetComponent(randomSprite);
    8.  
    9. }
    10.  
    11. function Update () {
    12.  
    13. }
    14.  
    15. function FixedUpdate() {
    16.     InputPress = Input.inputString;
    17.     if(Input.GetKeyDown) {
    18.         if(InputPress == "c") { // Here should be this letter C from sprite
    19.             Debug.Log("TRUE");
    20.         }
    21.         else {
    22.             Debug.Log("FALSE");
    23.         }
    24.     }
    25. }
     
  2. RSG

    RSG

    Joined:
    Feb 20, 2013
    Posts:
    93
    I'm guessing that each sprite represents a letter and you want to match the letter with the input from the keyboard?

    If this is the case, assuming that the name of a sprite matches the letter that it represents you could use something like:

    Code (CSharp):
    1. [Serializable]
    2. public class Test : MonoBehaviour
    3. {
    4.     [SerializeField]
    5.     public Sprite Sprite; // Name of the sprite is "A"
    6.  
    7.     private KeyCode SpriteKeyCode;
    8.  
    9.     private void Start()
    10.     {
    11.         // Convert the name of the sprite to a keycode,
    12.         // if the name cannot be converted, then this section will throw an exception
    13.         try
    14.         {
    15.             // Try to parse the name of the sprite into a
    16.             SpriteKeyCode = (KeyCode)Enum.Parse(typeof(KeyCode), Sprite.name);
    17.         }
    18.         catch
    19.         {
    20.             SpriteKeyCode = KeyCode.None;
    21.         }
    22.     }
    23.  
    24.     private void Update()
    25.     {
    26.         // Process only when a key is down and the name of the sprite was converted to a keycode
    27.         if(this.SpriteKeyCode != KeyCode.None && Input.GetKeyDown(this.SpriteKeyCode))
    28.         {
    29.             Debug.Log("Key is pressed: " + this.Sprite.name);
    30.         }
    31.     }
    32.  
    33.    
    34. }
     
    Mateusz Gryczan likes this.
  3. Mateusz Gryczan

    Mateusz Gryczan

    Joined:
    May 10, 2014
    Posts:
    10
    Thanks, that's what I mean. Could you give me an example in Javascript if you can :)
     
  4. RSG

    RSG

    Joined:
    Feb 20, 2013
    Posts:
    93
    Sorry I haven’t done JS in a while, but I think you should be able to port to JS from the logic.