При переходе к следующей сцене кнопка не нажимаетсяC#

Место общения программистов C#
Ответить
Anonymous
 При переходе к следующей сцене кнопка не нажимается

Сообщение Anonymous »

Я столкнулся с проблемой: когда все сделано в Сцене 1, я перехожу к Сцене 2. В сцене 2 есть кнопка и фон, но обе они не работают.
Вот изображение для сцены 1
Изображение

Вот изображение для сцены 2
Изображение

Ниже код прикреплен к кнопке, чтобы при нажатии игроком кнопки «Пуск» играть и начать игру.
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();
//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;
}
}
}

Вот код GameManager, который дает подробную информацию.
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!");

}

}

Ниже приведен код для обнаружения входных данных и сохранения данных в массиве. Это будет задействовано в сцене 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 playerNames = new List();
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 playerIDs = new List();
int i = 0;
foreach (TMP_InputField inputField in playerInputFields)
{
if (!string.IsNullOrEmpty(inputField.text))
{
playerIDs.Add((i + 1).ToString());
i++;
}
}
return playerIDs.ToArray();
}
}


Подробнее здесь: https://stackoverflow.com/questions/791 ... cant-click
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»