У меня тоже есть следующая проблема. Я создал этот скрипт, и когда я нажимаю кнопку «Купить», чтобы купить предмет, он деактивирует и возвращается к первому элементу вместо того, что я хочу. < /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
Проблема кода единства с активацией некоторых дочерних объектов [закрыто] ⇐ C#
Место общения программистов C#
1750268983
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[i] = transform.GetChild(i).gameObject;
spoilers[i].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);
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79666447/unity-code-problem-with-activating-some-child-objects[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия