Key not recognized

Hello,

I have a problem with the Input.KeyCode command. I need to set an action on a specific key, (which in my case is “c”), but it doesn’t respond to it. The code is not wrong since when I tested it with “d” or “q”, which are used for moving the character and thus are naturally recognized by the program, it worked. But when I use a key that is not used by default, like “y” or “c”, nothing happens.

here’s my code :

using UnityEngine;
using System.Collections;

public class Img10Accroupi : MonoBehaviour {

	private GameObject image;
	Texture3D texture;
    GUITexture guitexture;
		
	public float timer = 0.0f;
	
	public bool imageAffichee ;

	
	// Use this for initialization
	void Start () {
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	void OnTriggerEnter (Collider other) 
	{
		if (imageAffichee == false){
			if(other.tag == "Player" ) {		
				
				image = new GameObject("img");  
				image.AddComponent("GUITexture");
				image.guiTexture.texture = Resources.Load("10Accroupi") as Texture;
				image.transform.position = new Vector3(0.5f, 0.2f, 0.5f);
				image.transform.localScale = new Vector3(0.5F, 0.30F, 0.5F);
				
				imageAffichee = true ;	
			}
		}	
		
		if (imageAffichee == true)
		{	
			if (Input.GetKey("c")) 
			{
				timer += Time.deltaTime;
				if (timer >= 2.0) 
				{
					Destroy (gameObject);	
				}
			}
		}
	}
}

for the line “if (Input.GetKey(“c”))”, I also tried "if (Input.GetKey(KeyCode.C)).

Thank you for your answers !

I think you have a logic error here rather than issues with GetKey(). You are getting your key inside OnTriggerEnter(). This function will be called only once for each collision. Assuming a frame rate of 60 fps, that means you will have to have around 120 different collisions with the ‘C’ key held down before your game object is destroyed. Put a Debug.Log() statement between lines 42 and lines 43. You should see this body of code fire once for each collision. I’m not sure of your game mechanic is here, but you may want OnTriggerStay().

After testing a few things, adding a public bool to see where the problem was and make some tests again, it finally works!

Thanks a lot for your help !

Here’s the final code :

using UnityEngine;
using System.Collections;

public class Img10Accroupi : MonoBehaviour {

	private GameObject image;
	Texture3D texture;
    GUITexture guitexture;
		
	public float timer = 0.0f;
	
	public bool triggerEntree ;
	public bool destruction;

	
	// Use this for initialization
	void Start () {
	}
	
	// Update is called once per frame
	void Update () {
				
		if (triggerEntree == true)
		{	
			if (Input.GetKey("c")) 
			{
				destruction = true;
			}
		}
		
		if (destruction == true )
		{	
			timer += Time.deltaTime;
			if (timer >= 2.0) 
			{
				Destroy (GameObject.Find("img"));	
			}	
		}
	
	}
	
	void OnTriggerEnter (Collider other) {
		if (triggerEntree == false){
			if(other.tag == "Player" ) {		
				
				image = new GameObject("img");  
				image.AddComponent("GUITexture");
				image.guiTexture.texture = Resources.Load("10Accroupi") as Texture;
				image.transform.position = new Vector3(0.5f, 0.2f, 0.5f);
				image.transform.localScale = new Vector3(0.5F, 0.30F, 0.5F);
				
				triggerEntree = true ;	
			}
		}	
	}
}