1 скрипт:
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace ShopSystem
{
[CreateAssetMenu(fileName = "ShopData", menuName = "Resourses/ShopData")]
public class ShopSaveScriptable : ScriptableObject
{
public int selectedIndex;
public ShopItem[] itemsInfo;
}
[System.Serializable]
public class ShopItem
{
public int price;
public string name;
public bool isUnlocked;
}
}
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace ShopSystem
{
[CreateAssetMenu(fileName = "ShopData", menuName = "Resourses/ShopData")]
public class ShopSaveScriptable : ScriptableObject
{
public int selectedIndex;
public ShopItem[] itemsInfo;
}
[System.Serializable]
public class ShopItem
{
public int price;
public string name;
public bool isUnlocked;
}
}
И есть второй:
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using ShopSystem;
public class ShopController : MonoBehaviour
{
[Header("Coins")]
[Space(10f)]
[SerializeField] private int _coins;
[SerializeField] private Text _coinCounter;
[Header("Inheritance")]
[Space(10f)]
public ShopSaveScriptable ShopSaveScriptable;
public Slots slots;
[Header("Items")]
[Space(10f)]
[SerializeField] private GameObject[] _items;
[SerializeField] private Text _name;
[SerializeField] private GameObject _buyButton;
[SerializeField] private Text _priceText;
[SerializeField] private int _selectedIndex = 0;
[SerializeField] private int _currentIndex = 0;
[Header("Menu")]
[Space(10f)]
[SerializeField] private Button _nextButton;
[SerializeField] private Button _previousButton;
private void Start()
{
_selectedIndex = ShopSaveScriptable.selectedIndex;
_currentIndex = _selectedIndex;
_coinCounter.text = _coins.ToString();
_items[_currentIndex].SetActive(true);
SetCarInfo();
_nextButton.onClick.AddListener(() => GoToNextItem());
_previousButton.onClick.AddListener(() => GoToPreviousItem());
}
private void SetCarInfo()
{
_name.text = ShopSaveScriptable.itemsInfo[_currentIndex].name;
_priceText.text = ShopSaveScriptable.itemsInfo[_currentIndex].price.ToString();
}
public void GoToNextItem()
{
if(_currentIndex < ShopSaveScriptable.itemsInfo.Length - 1)
{
_items[_currentIndex].SetActive(false);
_currentIndex++;
_items[_currentIndex].SetActive(true);
if(_currentIndex == ShopSaveScriptable.itemsInfo.Length - 1)
{
_nextButton.interactable = false;
}
if(!_nextButton.interactable)
{
_previousButton.interactable = true;
}
}
}
public void GoToPreviousItem()
{
if (_currentIndex > 0)
{
_items[_currentIndex].SetActive(false);
_currentIndex--;
_items[_currentIndex].SetActive(true);
if (_currentIndex == 0)
{
_previousButton.interactable = false;
}
if (!_previousButton.interactable)
{
_nextButton.interactable = true;
}
}
}
/*public void BuyItem(int index)
{
if (_thing[index].CompareTag("Furniture"))
{
for (int i = 0; i < slots._slotsFurnitureInventory.Length; i++)
{
if (slots.isUsedFurnitureInventory[i] == false)
{
slots.isUsedFurnitureInventory[i] = true;
Instantiate(_thing[index], slots._slotsFurnitureInventory[i].transform);
break;
}
}
}
else if (_thing[index].CompareTag("Clothes"))
{
for (int i = 0; i < slots._slotsClothesInventory.Length; i++)
{
if (slots.isUsedClothesInventory[i] == false)
{
slots.isUsedClothesInventory[i] = true;
Instantiate(_thing[index], slots._slotsClothesInventory[i].transform);
break;
}
}
}
else if (_thing[index].CompareTag("Plot"))
{
for (int i = 0; i < slots._slotsClothesInventory.Length; i++)
{
if (slots.isUsedPlotInventory[i] == false)
{
slots.isUsedPlotInventory[i] = true;
Instantiate(_thing[index], slots._slotsPlotInventory[i].transform);
break;
}
}
}
else if (_thing[index].CompareTag("OtherObjectsInventory"))
{
for (int i = 0; i < slots._slotsOtherInventory.Length; i++)
{
if (slots.isUsedOtherInventory[i] == false)
{
slots.isUsedOtherInventory[i] = true;
Instantiate(_thing[index], slots._slotsOtherInventory[i].transform);
break;
}
}
}
}*/
}
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using ShopSystem;
public class ShopController : MonoBehaviour
{
[Header("Coins")]
[Space(10f)]
[SerializeField] private int _coins;
[SerializeField] private Text _coinCounter;
[Header("Inheritance")]
[Space(10f)]
public ShopSaveScriptable ShopSaveScriptable;
public Slots slots;
[Header("Items")]
[Space(10f)]
[SerializeField] private GameObject[] _items;
[SerializeField] private Text _name;
[SerializeField] private GameObject _buyButton;
[SerializeField] private Text _priceText;
[SerializeField] private int _selectedIndex = 0;
[SerializeField] private int _currentIndex = 0;
[Header("Menu")]
[Space(10f)]
[SerializeField] private Button _nextButton;
[SerializeField] private Button _previousButton;
private void Start()
{
_selectedIndex = ShopSaveScriptable.selectedIndex;
_currentIndex = _selectedIndex;
_coinCounter.text = _coins.ToString();
_items[_currentIndex].SetActive(true);
SetCarInfo();
_nextButton.onClick.AddListener(() => GoToNextItem());
_previousButton.onClick.AddListener(() => GoToPreviousItem());
}
private void SetCarInfo()
{
_name.text = ShopSaveScriptable.itemsInfo[_currentIndex].name;
_priceText.text = ShopSaveScriptable.itemsInfo[_currentIndex].price.ToString();
}
public void GoToNextItem()
{
if(_currentIndex < ShopSaveScriptable.itemsInfo.Length - 1)
{
_items[_currentIndex].SetActive(false);
_currentIndex++;
_items[_currentIndex].SetActive(true);
if(_currentIndex == ShopSaveScriptable.itemsInfo.Length - 1)
{
_nextButton.interactable = false;
}
if(!_nextButton.interactable)
{
_previousButton.interactable = true;
}
}
}
public void GoToPreviousItem()
{
if (_currentIndex > 0)
{
_items[_currentIndex].SetActive(false);
_currentIndex--;
_items[_currentIndex].SetActive(true);
if (_currentIndex == 0)
{
_previousButton.interactable = false;
}
if (!_previousButton.interactable)
{
_nextButton.interactable = true;
}
}
}
/*public void BuyItem(int index)
{
if (_thing[index].CompareTag("Furniture"))
{
for (int i = 0; i < slots._slotsFurnitureInventory.Length; i++)
{
if (slots.isUsedFurnitureInventory[i] == false)
{
slots.isUsedFurnitureInventory[i] = true;
Instantiate(_thing[index], slots._slotsFurnitureInventory[i].transform);
break;
}
}
}
else if (_thing[index].CompareTag("Clothes"))
{
for (int i = 0; i < slots._slotsClothesInventory.Length; i++)
{
if (slots.isUsedClothesInventory[i] == false)
{
slots.isUsedClothesInventory[i] = true;
Instantiate(_thing[index], slots._slotsClothesInventory[i].transform);
break;
}
}
}
else if (_thing[index].CompareTag("Plot"))
{
for (int i = 0; i < slots._slotsClothesInventory.Length; i++)
{
if (slots.isUsedPlotInventory[i] == false)
{
slots.isUsedPlotInventory[i] = true;
Instantiate(_thing[index], slots._slotsPlotInventory[i].transform);
break;
}
}
}
else if (_thing[index].CompareTag("OtherObjectsInventory"))
{
for (int i = 0; i < slots._slotsOtherInventory.Length; i++)
{
if (slots.isUsedOtherInventory[i] == false)
{
slots.isUsedOtherInventory[i] = true;
Instantiate(_thing[index], slots._slotsOtherInventory[i].transform);
break;
}
}
}
}*/
}
Суть проблемы: на 36 строчке выдаёт ошибку NullReferenceException: Object reference not set to an instance of an object
ShopController.Start () (at Assets/Scripts/Shop/ShopController.cs:36)
Как я понял, это связано с переменной ShopSaveScriptable.selectedIndex. Помогите, пожалуйста!