Reputation: 11
I encounter an issue that when everything is done in Scene1 and go to Scene2. There a button in Scene 2 also there bgm but both of them are not working.
Here is an image for scene 1
Here is an image for scene 2
Below code are attached with the button so that when player click start will play and start the game.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;
public class CameraManager : MonoBehaviour
{
// Import button &
//public Camera camera;
public Camera[] cameras;
public GameObject[] detectors;
public Button button;
public GameObject divider;
public GameObject leaderboard;
public Animator animator;
public GameManager gameManager;
private LeaderboardManager leaderboardManager;
private int currentCameraIndex = 0;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
//camera = cameras[0];
cameras[0].enabled = true;
button.gameObject.SetActive(true);
Debug.Log("Button is Actived");
Debug.Log("Button is interactable: " + button.interactable);
}
// Update is called once per frame
void Update()
{
}
//if button onCLick() switch camera
public void buttonOnClick()
{
Debug.Log("pressed");
// Hide start button
button.gameObject.SetActive(false);
//show Leaderboad
leaderboard.gameObject.SetActive(true);
animator.SetTrigger("Start Anim");
divider.gameObject.SetActive(false);
switchToNextCamera();
}
public void switchToNextCamera()
{
// Disable the current camera
cameras[currentCameraIndex].enabled = false;
// Move to the next camera, looping back to the start if necessary
//currentCameraIndex = (currentCameraIndex + 1) % cameras.Length;
currentCameraIndex = currentCameraIndex + 1;
// Enable the new camera
cameras[currentCameraIndex].enabled = true;
Debug.Log("Switched to camera index: " + currentCameraIndex);
}
// Detect collision with a detector and switch to the corresponding camera
void OnTriggerEnter(Collider other)
{
Debug.Log("Something entered the trigger: " + other.gameObject.name);
}
public void switchToSpecificCamera(int index)
{
// Ensure the index is within bounds
if (index >= 0 && index < cameras.Length)
{
// Disable the current camera
cameras[currentCameraIndex].enabled = false;
// Switch to the specified camera
currentCameraIndex = index;
cameras[currentCameraIndex].enabled = true;
}
}
}
Here is code about GameManager that get the details.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public static GameManager Instance;
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject); // Keep the GameManager alive across scenes
}
else
{
Destroy(gameObject); // If another GameManager exists, destroy this one
}
}
// Start is called before the first frame update
void Start()
{
getPlayerNames();
}
// Update is called once per frame
void Update()
{
}
public string[] getPlayerNames()
{
if (FileUploaderCrossPlatform.isUsingFile == true)
{
string[] playerName = FileUploaderCrossPlatform.PlayerName;//ReadData correct
return playerName;
}
else if (FileUploaderCrossPlatform.isUsingFile == false)
{
string[] playerName = MainMenu.Instance.readPlayerName();
return playerName;
}
else
{
Debug.LogWarning("Player names not loaded yet.");
return new string[0]; // Return an empty array if no data is available
}
}
public string[] getPlayerID()
{
if (FileUploaderCrossPlatform.isUsingFile == true)
{
string[] playerID = FileUploaderCrossPlatform.PlayerID;//Read data correct
return playerID;
}
else if(FileUploaderCrossPlatform.isUsingFile == false)
{
string[] playerID = MainMenu.Instance.readPlayerID();//Read data correct
return playerID;
}
else
{
Debug.LogWarning("Player ID not loaded yet.");
return new string[0]; // Return an empty array if no data is available
}
}
public int getNumberOfPlayer()
{
if (FileUploaderCrossPlatform.isUsingFile == true)
{
// Return the array of player
int numberOfPlayers = FileUploaderCrossPlatform.NumberOfPlayers; // Access static variable
Debug.Log("Number of players(File): " + numberOfPlayers);
return numberOfPlayers;
}
else if (FileUploaderCrossPlatform.isUsingFile == false)
{
//Empty file read input
int numberOfPlayers = MainMenu.Instance.readNumberOfPlayers();
Debug.Log("Number of players (Input): " + numberOfPlayers);
return numberOfPlayers;
}
else
{
Debug.LogWarning("Num of Player not loaded yet.");
return 0; // Return an empty array if no data is available
}
}
public void OnBallPassed()
{
Debug.Log("The ball has passed the trigger point!");
}
}
Below is code about detect the input and save the details into array.This will be attact under Scene 1.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using TMPro;
using System.IO;
public class MainMenu : MonoBehaviour
{
public TMP_InputField[] playerInputFields;
public static MainMenu Instance;
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject); // If another MainMenu exists, destroy this one
}
}
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void loadnext()
{
//SceneManager.LoadScene("Track1");
SceneManager.LoadScene("Track2");
}
public int readNumberOfPlayers()
{
int numberOfPlayers = 0;
foreach (TMP_InputField inputField in playerInputFields)
{
if (!string.IsNullOrEmpty(inputField.text))
{
numberOfPlayers++;
}
}
return numberOfPlayers;
}
public string[] readPlayerName()
{
List<string> playerNames = new List<string>();
foreach (TMP_InputField inputField in playerInputFields)
{
if (!string.IsNullOrEmpty(inputField.text))
{
playerNames.Add(inputField.text); // Add the name if it's not empty
}
}
return playerNames.ToArray();
}
public string[] readPlayerID()
{
List<string> playerIDs = new List<string>();
int i = 0;
foreach (TMP_InputField inputField in playerInputFields)
{
if (!string.IsNullOrEmpty(inputField.text))
{
playerIDs.Add((i + 1).ToString());
i++;
}
}
return playerIDs.ToArray();
}
}
Upvotes: 0
Views: 41
Reputation: 138
I think I see the problem, if the button isn't responding, it's likely because there's no event listener tied to it. instead of having just buttonOnClick()
sitting there, you need to add an event listener to your button in Start()
.
Try adding this line to Start()
to link the button click event to the buttonOnClick()
method: button.onClick.AddListener(buttonOnClick);
Upvotes: 0