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:
#pragma strict
var countDownScript : countDown;
var sprites : Sprite[];
var currentSprite : int = -1;
var startRandomLetters : boolean = false;
function Start () {
countDownScript = GameObject.FindGameObjectWithTag("MainCamera").gameObject.GetComponent(countDown);
}
function FixedUpdate() {
if(countDownScript.timer == 0) {
startRandomLetters = true;
}
if(currentSprite == -1 && startRandomLetters) {
currentSprite = sprites.Length + 1;
currentSprite = Random.Range(0, sprites.Length);
GetComponent(SpriteRenderer).sprite = sprites[currentSprite];
}
}
Testing:
#pragma strict
var randomSpriteScript : randomSprite;
var InputPress;
function Start () {
randomSpriteScript = GameObject.FindGameObjectWithTag("Letter").gameObject.GetComponent(randomSprite);
}
function Update () {
}
function FixedUpdate() {
InputPress = Input.inputString;
if(Input.GetKeyDown) {
if(InputPress == "c") { // Here should be this letter C from sprite
Debug.Log("TRUE");
}
else {
Debug.Log("FALSE");
}
}
}
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:
[Serializable]
public class Test : MonoBehaviour
{
[SerializeField]
public Sprite Sprite; // Name of the sprite is "A"
private KeyCode SpriteKeyCode;
private void Start()
{
// Convert the name of the sprite to a keycode,
// if the name cannot be converted, then this section will throw an exception
try
{
// Try to parse the name of the sprite into a
SpriteKeyCode = (KeyCode)Enum.Parse(typeof(KeyCode), Sprite.name);
}
catch
{
SpriteKeyCode = KeyCode.None;
}
}
private void Update()
{
// Process only when a key is down and the name of the sprite was converted to a keycode
if(this.SpriteKeyCode != KeyCode.None && Input.GetKeyDown(this.SpriteKeyCode))
{
Debug.Log("Key is pressed: " + this.Sprite.name);
}
}
}