Проблема кода единства с активацией некоторых дочерних объектов [закрыто]C#

Место общения программистов C#
Ответить
Anonymous
 Проблема кода единства с активацией некоторых дочерних объектов [закрыто]

Сообщение Anonymous »

У меня тоже есть следующая проблема. Я создал этот скрипт, и когда я нажимаю кнопку «Купить», чтобы купить предмет, он деактивирует и возвращается к первому элементу вместо того, что я хочу. < /P>
public GameObject wheelBuyMenu;`enter code here`
public Button nextButton;
public Button previousButton;
public Text spoilerNumberText;
public GameObject buyPanel;
public Text buyText;

private GameObject[] spoilers;
private int currentIndex = 0;
private string currentCar = "DefaultCar";

private void Awake()
{
int count = transform.childCount;
spoilers = new GameObject[count];
for (int i = 0; i < count; i++)
{
spoilers = transform.GetChild(i).gameObject;
spoilers.SetActive(false);
}
}

private void Start()
{

int savedIndex = PlayerPrefs.GetInt("SpoilerSelected_" + currentCar, -1);
if (savedIndex >= 0 && IsPurchased(savedIndex))
{
currentIndex = savedIndex;
}
else
{
currentIndex = 0;
}

UpdateSpoilerDisplay();
}

public void SwitchSpoiler(int direction)
{
currentIndex += direction;
currentIndex = Mathf.Clamp(currentIndex, 0, spoilers.Length - 1);
UpdateSpoilerDisplay();
}

public void BuySpoiler(int price)
{
if (CashSistem.TotalCash >= price)
{
CashSistem.TotalCash -= price;

SetPurchased(currentIndex);
PlayerPrefs.SetInt("SpoilerSelected_" + currentCar, currentIndex);
PlayerPrefs.Save();

buyText.text = "Spoiler purchased!";
buyText.color = Color.green;
}
else
{
buyText.text = "Not enough money!";
buyText.color = Color.red;
}

UpdateSpoilerDisplay();
}

private void UpdateSpoilerDisplay()
{
foreach (GameObject spoiler in spoilers)
{
spoiler.SetActive(false);
}

if (IsPurchased(currentIndex))
{
spoilers[currentIndex].SetActive(true);
buyPanel.SetActive(false);
}
else
{
buyPanel.SetActive(true);
}

spoilerNumberText.text = "Spoiler: " + (currentIndex + 1);
previousButton.interactable = currentIndex > 0;
nextButton.interactable = currentIndex < spoilers.Length - 1;
}

private bool IsPurchased(int index)
{
return PlayerPrefs.GetInt("SpoilerPurchased_" + currentCar + "_" + index, 0) == 1;
}

private void SetPurchased(int index)
{
PlayerPrefs.SetInt("SpoilerPurchased_" + currentCar + "_" + index, 1);
}

public void BuyButoonManager()
{
wheelBuyMenu.SetActive(true);
}


Подробнее здесь: https://stackoverflow.com/questions/796 ... ld-objects
Ответить

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

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

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

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

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