Hello,
I have been struggling for weeks with local coop.
My issue is that all my controllers controls the first player… even in the main menu of my game.
I am quite a beginner at unity. could someone help. Here is the script and screenshots of my setup.
Thanks a lot for your help.
using System.Collections;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class MainMenuController : MonoBehaviour
{
public GameObject startButton;
public Button optionsButton;
public Button quitButton;
public float loadDelay = 0.5f;
void Start()
{
if (startButton != null && optionsButton != null && quitButton != null)
{
// Set up navigation
Navigation startNav = startButton.GetComponent<Button>().navigation;
startNav.mode = Navigation.Mode.Explicit;
startNav.selectOnDown = optionsButton;
startButton.GetComponent<Button>().navigation = startNav;
Debug.Log("Start Button navigation set.");
Navigation optionsNav = optionsButton.navigation;
optionsNav.mode = Navigation.Mode.Explicit;
optionsNav.selectOnUp = startButton.GetComponent<Button>();
optionsNav.selectOnDown = quitButton;
optionsButton.navigation = optionsNav;
Debug.Log("Options Button navigation set.");
Navigation quitNav = quitButton.navigation;
quitNav.mode = Navigation.Mode.Explicit;
quitNav.selectOnUp = optionsButton;
quitButton.navigation = quitNav;
Debug.Log("Quit Button navigation set.");
// Set the default selected button
EventSystem.current.SetSelectedGameObject(startButton);
Debug.Log("Default selected button set to Start Button.");
}
else
{
Debug.LogError("Buttons are not assigned in the Inspector.");
}
}
void OnEnable()
{
if (startButton != null)
{
// Make sure the button is selected again when the menu is enabled
EventSystem.current.SetSelectedGameObject(startButton);
}
}
public void OnMove(InputAction.CallbackContext context)
{
Vector2 input = context.ReadValue<Vector2>();
// Filter out left and right movements
if (Mathf.Abs(input.x) > Mathf.Abs(input.y))
{
return;
}
Debug.Log($"OnMove triggered with input: {input}");
// Get the currently selected game object
GameObject currentSelected = EventSystem.current.currentSelectedGameObject;
if (currentSelected != null)
{
Debug.Log($"Current selected: {currentSelected.name}");
if (input != Vector2.zero)
{
Debug.Log($"Processing move input: {input}");
// Move selection
AxisEventData axisEventData = new AxisEventData(EventSystem.current)
{
moveVector = input,
moveDir = DetermineMoveDirection(input.x, input.y)
};
ExecuteEvents.Execute(currentSelected, axisEventData, ExecuteEvents.moveHandler);
GameObject newSelected = EventSystem.current.currentSelectedGameObject;
if (newSelected != null)
{
Debug.Log($"Moved selection to: {newSelected.name}");
}
else
{
Debug.LogWarning("No button is currently selected.");
EventSystem.current.SetSelectedGameObject(currentSelected);
}
}
}
else
{
Debug.LogWarning("No button is currently selected.");
}
}
private MoveDirection DetermineMoveDirection(float x, float y)
{
if (Mathf.Abs(x) > Mathf.Abs(y))
{
return x > 0 ? MoveDirection.Right : MoveDirection.Left;
}
else
{
return y > 0 ? MoveDirection.Up : MoveDirection.Down;
}
}
public void OnSubmit(InputAction.CallbackContext context)
{
Debug.Log("OnSubmit triggered");
// Handle submit input
if (gameObject.activeInHierarchy) // Ensure the GameObject is active
{
OnStartButtonClicked();
}
}
public void OnCancel(InputAction.CallbackContext context)
{
Debug.Log("OnCancel triggered");
// Handle cancel input
}
public void OnStartButtonClicked()
{
// Start the coroutine to load the scene with a delay
StartCoroutine(LoadSceneWithDelay("NextSceneName"));
}
private IEnumerator LoadSceneWithDelay(string sceneName)
{
// Play the click sound (assuming you have an AudioSource component attached to the button or another GameObject)
AudioSource audioSource = GetComponent<AudioSource>();
if (audioSource != null)
{
audioSource.Play();
}
// Wait for the delay
yield return new WaitForSeconds(loadDelay);
// Load the next scene
SceneManager.LoadScene(sceneName);
}
}


