- Home /
How can I display an object with a script based off an int in a different script?
I'm making a simple RPG for practice purposes and I want to count the number of dungeons the player beat. Based off of that number, I want to let the player buy different items.
Each item has a class where it has a public getter and setter saying how many dungeons must be beaten to display the item. How can I use a script to control all of the items that can be purchased with the int dungeonsBeaten?
why not just make a script with static variables?
I personally would make a public array of structs with all information needed for an item (name, id, icon, dungeons needed etc, as well as a canBuy bool), and add the items using the inspector (or save them in a file). When loading them, check if the player has beaten enough dungeons and set the canBuy to true or false depending on it.
EDIT: Of course, you'll have to either reload or recheck for the dungeons beaten every time you beat another dungeon.
Answer by kornstar83 · Jun 10, 2017 at 08:20 PM
Have a script that checks the dungeonBeaten int compared to the Items requiredDungeons value whenever the shop is opened and make a list of available items for purchase.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;//required to use Lists
public class DungeonCountController : MonoBehaviour {
public Item[] allItems; //Assign the items in your inspector
public List<Item> availableItems;
// Use this for initialization
void Start () {
availableItems = new List<Item> ();
}
public void DungeonCheck(){// call this when the shop is opened
availableItems.Clear();
foreach (Item item in allItems) {
if (dungeonsBeaten >= item.requiredDungeons ) {
availableItems.Add (item);
}
}
}
}
You can then use the contents of the availableItems list to display all the items the player can buy with their current number of dungeonsBeaten.
What would I put for the "item.requiredDungeons" on line 17?
In this case, requiredDungeons would be an int in the Item class, which would be just how many dungeons the specific item requires the player to beat
I've got the whole script written out, but, for some reason, the Item[] allItems array is not showing up in my inspector, so I can't assign items. Am I missing something?
$$anonymous$$ake sure you make it is public, and it should be on the inspector. If it is not, try doing [SerializeField] before the variable.
[SerializeField]
public Items[] allItems;
This is what I have and, even with the [SerializeField] it still doesn't work
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShopContents : $$anonymous$$onoBehaviour {
public ItemClass[] allItems;
public List<ItemClass> availableItems;
void Start()
{
availableItems = new List<ItemClass>();
DungeonCheck();
}
void DungeonCheck()
{
availableItems.Clear();
foreach (ItemClass item in allItems)
{
if (PlayerStat$$anonymous$$anager.dungeonsBeaten >= item.DungeonUnlock)
{
availableItems.Add(item);
}
}
}
}
I only used GameObject as an example. GameObject IS a class, so, you'd just need to replace GameObject with your own class.
Okay, I see. Your screenshot didn't load, so I couldn't see what you did either
at the top of your ItemClass script put this,
[System.Serializable]
public class ItemClass : $$anonymous$$onoBehaviour// ... yadda yadda yadda.
$$anonymous$$aking the itemclass itself serializable might have it show up better in the inspector.
Yeah, that's what I have, but I can't change the elements, just the size: https://gyazo.com/8a802c2e2e6f271aeb6a33a3ced069fb