0
\$\begingroup\$

I want to modify an int variable on one script by having another script subtract 1 from it. But when I try to trigger this on an "enter" key press, nothing happens.

1st script:

public class spawnenemy : MonoBehaviour {

    public int enems = 1;

2nd script:

public class collisions : MonoBehaviour {

private spawnenemy enems;

void Start () {
    enems = gameObject.GetComponent<spawnenemy> ();

  }

void FixedUpdate(){
  if (Input.GetKey("enter")){
   enems.enems--;
 }
}

Thanks.

\$\endgroup\$
  • \$\begingroup\$ Are both scripts on the same gameObject? Did you tried put some Debug.Log() into if (Input.GetKey("enter"))? \$\endgroup\$ – Paweł Marecki Mar 23 '16 at 13:10
3
\$\begingroup\$

"enter" is not a key name that Unity recognizes.

I recommend using the KeyCode enumeration instead of raw strings, so your IDE's autocompletion and the compiler will tell you when you're trying to use an unknown key identifier. In this case you'd use...

if(Input.GetKey(KeyCode.Return)) ...

If you prefer to use strings, refer to the Unity docs for the string names of each key:

Keys

The names of keys follow this convention:

  • Normal keys: “a”, “b”, “c” …

  • Number keys: “1”, “2”, “3”, …

  • Arrow keys: “up”, “down”, “left”, “right”

  • Keypad keys: “[1]”, “[2]”, “[3]”, “[+]”, “[equals]”

  • Modifier keys: “right shift”, “left shift”, “right ctrl”, “left ctrl”, “right alt”, “left alt”, “right cmd”, “left cmd”

  • Mouse Buttons: “mouse 0”, “mouse 1”, “mouse 2”, …

  • Joystick Buttons (from any joystick): “joystick button 0”, “joystick button 1”, “joystick button 2”, …

  • Joystick Buttons (from a specific joystick): “joystick 1 button 0”, “joystick 1 button 1”, “joystick 2 button 0”, …

  • Special keys: “backspace”, “tab”, “return”, “escape”, “space”, “delete”, “enter”, “insert”, “home”, “end”, “page up”, “page down”

  • Function keys: “f1”, “f2”, “f3”, …

\$\endgroup\$
  • \$\begingroup\$ Some debugging help for future: try putting a breakpoint or a Debug.Log("Wow, this line executed!"); inside any if statement that doesn't seem to be having any effect. That will help you determine whether the problem is a) the code inside is ineffective or b) the conditions of the if are never satisfied, so the code never executes at all. That narrows the problem space so you can debug it yourself, or ask more targeted questions. In this case, the cross-script access in your question was a red herring, and wasn't related to the real problem. \$\endgroup\$ – DMGregory Mar 23 '16 at 17:26
1
\$\begingroup\$

Input.GetKeyDown("enter") uses the enter key on the number pad.

\$\endgroup\$
  • \$\begingroup\$ Hello and welcome to Gamedev.SE! Its bit unclear for me, why exactly you are getting downvoted and reported, when your answer is exactly the solution to this problem. Enter key is not same as return key, thus making your answer spot on. A bit short answer maybe, but still absolutely correct. \$\endgroup\$ – Katu Sep 7 '18 at 5:49
0
\$\begingroup\$
    //player propertie
    public float speed;
    public int health;
    public int score;
    //bullet
    public Bullet bulletPrefab;
    public Transform fireposition;
    //Sounds
    public AudioClip fxFire;
    //Boundaries
    [SerializeField] private float maxX;
    [SerializeField] private float minX;
    // Use this for initialization

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
            FireBullet();

        if (Input.GetKey(KeyCode.RightArrow))
            transform.position += Vector3.right * speed * Time.deltaTime;
        else if (Input.GetKey(KeyCode.LeftArrow))
            transform.position += Vector3.left * speed * Time.deltaTime;

        //Calculate 
        float newX = Mathf.Clamp(transform.position.x, minX, maxX);
        transform.position = new Vector3(newX, transform.position.y, transform.position.z);
    }

    void FireBullet()
    {
        AudioSource.PlayClipAtPoint(fxFire, transform.position);
        Instantiate(bulletPrefab, fireposition.transform.position, Quaternion.identity);
    }
}
\$\endgroup\$
  • \$\begingroup\$ Did you forget to describe what's going here, and why it solves the question asked? \$\endgroup\$ – Vaillancourt Jul 30 '18 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.