Pressing space calls the wrong function

Hi guys,

the game I am woring on has two functions so far: Opening a menu by pressing space and instantiating objects when UI-buttons in the menu are pressed. The script for the menu animation is on the FPS and the scripts for spawning the objects are on an empty game object.

The problem is that when I use the spacebar to open or close the menu it now also spawns objects, even though I didn’t press any buttons. This only happens once I pressed a button, not when no buttons were pressed during a testrun.

I don’t really see why this happens or where the connection between the spacebar and the buttons is.

Here is the scipt for the menu:

using UnityEngine;
using System.Collections;

public class MenuAnimS : MonoBehaviour {
	bool MenuOpen;
	Animator animator1;

	void Start () {
		animator1 = GameObject.Find("Canvas").GetComponent<Animator>();
		animator1.enabled = false;
		MenuOpen = false;
	}
	

	void Update () {
		if (Input.GetKeyUp ("space") && MenuOpen == false) {
			animator1.enabled = true;
			animator1.Play ("MenueAnim");
			MenuOpen = true;
		} else if (Input.GetKeyUp("space") && MenuOpen == true){
			animator1.Play ("MenueAnim2");
			MenuOpen = false;
		}
	}
}

and this is the script for the buttons:

using UnityEngine;
using System.Collections;

public class SR : MonoBehaviour {

	private GameObject objCamera;
	private Vector3 SpawnPosition;
	private int DistanceToCamera = 5;
	public Transform creation;
	public Transform player;
	
	void Start () {
		objCamera = (GameObject) GameObject.FindWithTag("MainCamera");
	}
	
public	void Spwn () {
		SpawnPosition = objCamera.transform.forward * DistanceToCamera + objCamera.transform.position ;
		Instantiate(creation, SpawnPosition, player.transform.rotation);
	}
}

Does anyone see what I’ve missed?

Thanks :slight_smile:

That is because the button is selected (In playmode, click the EventSystem, in the inspector, in the preview (The darker area near the bottom) you should see:

Selected:YourButtonNameHere

And in the standard input module you can see that the submit button is set to “Submit”. If you then go to “Edit/Project Settings/Input” you can look at submit (Is there twice by default) and one of them has “alt positive button” set to “space”. As you can see, return is the other one that triggers it, aswell as joystick button 0.

So pressing space clicks the button if it is selected. The button is still selected because it was the last button you clicked :wink:

I believe setting the Button’s navigation to “None” should fix it. (Obviously, if you do that navigation won’t work for that button anymore)