- Home /
How do I make the objects on my list activate according to the variable?
I would like to ask a question, I am making a game of ships, I was creating a list with the objects (slots) that would be disabled initially, however they would be activated according to the number of slots stored in the variable, I put it in the script as public and added it in the inspector the objects on the scene in their proper places in the list, but this error appears: "Index was outside the bounds of the array". What would I need to do for this to work?
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class ItensId : MonoBehaviour {
public GameObject[] Slot;
public List<Text> IdText;
private bool InventoryFull;
void Start()
{
InventoryFull = false;
DataBase.CountItens = PlayerPrefs.GetInt("CountItens");
}
// Update is called once per frame
void Update()
{
if(ItensContent.buy == true && InventoryFull == false)
{
Slot[DataBase.CountItens].SetActive(true);
ItensContent.buy = false;
}
}
} obs: The calculation of the items is done in a variable from another script.
Answer by cam74 · Apr 30, 2020 at 10:47 PM
@Animaki An array starts from 0 ang goes to n-1 with n being the number of thing in your array. You should add - 1 to your 16th line after database.countitems. If you have 3 items the 3rd item will be slot[2] Because: Slot[0] Slot[1] Slot[2]->the third object
Vote up if I answered you
Unfortunately not, this is the script that calculates CountItens, it works from a button to select the item (S$$anonymous$$S) and the other to buy the item (Buy). This and the other script are in different scenes. If this information helps you to identify the problem, I am grateful.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class ItensContent : $$anonymous$$onoBehaviour { static public string id; private string ChosenItem; static public bool buy;
void Start()
{
buy = false;
}
public void Purchase()
{
DataBase.CountItens = DataBase.CountItens + 1;
PlayerPrefs.SetInt("CountItens", DataBase.CountItens);
id = ChosenItem;
buy = true;
}
public void Sms()
{
ChosenItem = "sms";
}
public void Home()
{
UnityEngine.Scene$$anonymous$$anagement.Scene$$anonymous$$anager.LoadScene("Home");
}
}
obs: The CountItens variable is in a static script serving only as a stored database.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How can I make multiple of entry for arraylist in inspector 2 Answers
How to acces the value of List of arrays 0 Answers
Is it possible to sort an array determined by an external comparison? 1 Answer
Add a Play Next Song And A shuffle Option To Music Player C# 1 Answer