Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MrAwesome343 · Jun 10, 2017 at 07:26 PM · classintrpgsettersgetters

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image toddisarockstar · Jun 10, 2017 at 07:36 PM 0
Share

why not just make a script with static variables?

avatar image FunIsDangerous · Jun 10, 2017 at 07:44 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 13 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MrAwesome343 · Jun 10, 2017 at 11:08 PM 0
Share

What would I put for the "item.requiredDungeons" on line 17?

avatar image FunIsDangerous MrAwesome343 · Jun 11, 2017 at 06:02 AM 0
Share

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

avatar image MrAwesome343 · Jun 11, 2017 at 05:22 PM 0
Share

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?

avatar image FunIsDangerous MrAwesome343 · Jun 11, 2017 at 05:45 PM 0
Share

$$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;
avatar image MrAwesome343 FunIsDangerous · Jun 11, 2017 at 05:57 PM 0
Share

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);
             }
         }
     }
 
 }
Show more comments
Show more comments
avatar image FunIsDangerous MrAwesome343 · Jun 11, 2017 at 08:54 PM 0
Share

I only used GameObject as an example. GameObject IS a class, so, you'd just need to replace GameObject with your own class.

avatar image MrAwesome343 FunIsDangerous · Jun 11, 2017 at 09:26 PM 0
Share

Okay, I see. Your screenshot didn't load, so I couldn't see what you did either

Show more comments
avatar image ClearRoseOfWar MrAwesome343 · Jun 11, 2017 at 11:51 PM 0
Share

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.

avatar image MrAwesome343 ClearRoseOfWar · Jun 11, 2017 at 11:59 PM 0
Share

Yeah, that's what I have, but I can't change the elements, just the size: https://gyazo.com/8a802c2e2e6f271aeb6a33a3ced069fb

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

71 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

problems with setters and getters. 1 Answer

How do you make RPG class stat presets? 1 Answer

getter and setter not working 1 Answer

Accessing class properties through getters & setters in editor mode 1 Answer

Abstract Inheritance and abstract variables sloppy code 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges