• 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 matthew_gigante · Oct 16, 2017 at 03:13 PM · textlistinventoryinventory systemitems

How to make an inventory for my text advenutre?

I am creating a text adventure game, and using strings and text instead of traditional objects. Below I have my script that handles all events/searches for items in game (It probably looks very intimidating since it is made to work for many different scenarios).

What I would like to happen when an "item" (string) is found in game, for it to be added to the inventory, w$$anonymous$$ch would be a separate scene that can be reached from any scene, but the inventory contents itself would be an object in every scene, for use in gathering items/selling/buying etc. Here is my code and how I load my scenes:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 //IMPORTANT NOTE TO SELF: REPLACE ITEMSFOUND LIST WITH INVENTORY LIST, SO THAT ITEMS MAY BE ADDED TO INVENTORY
 
 //NOTES ON THIS SCRIPT: t$$anonymous$$s script may seem uneccesary, and is fairly complicated in the number of public variables being thrown about,
 //but I am using many public variables to maximize reusability and in the long run reduce the file size of t$$anonymous$$s project.
 
 public class itemGenerator : MonoBehaviour {
     //sound plays on find items
     public AudioSource items;
     //sound plays on finding not$$anonymous$$ng
     public AudioSource noItems;
 
     //The following 3 strings are so that I may use t$$anonymous$$s script for other items in the future
     //item1 to find
     public string item1;
     //item2 to find
     public string item2;
     //item3 to find
     public string item3;
 
     //text in the button to continue may change depending on the situation, these strings handle that
     public string textInButton0;
     public string textInButton1;
 
     //allows me to drag any text from my scene to use as the textInButton text
     public Text textInButton;
 
     //allows me to drag any text from my scene to use as the displayItemsFound text
     public Text displayItemsFound;
 
     //list to display items found
     public List<string> itemsFound = new List<string>();
 
     //t$$anonymous$$s will handle how many items will be found during each run of the script
     int numberOfItems;
 
     //makes sure the right amount of items is spawned
     int itemsSpawned;
 
     //uses integer values and assigns them to a string name for the items
     int itemType;
 
     //min range is usually set to 0, unless I always want the player to find an item or have an event occur
     public int minRange;
     //max range is equal to one more than the number of items I want
     public int maxRange;
 
     //these options are made public so that I may use t$$anonymous$$s script for different items in different scenes
     //startStatement allows me to manipulate what the text says before displaying items/events
     public string startStatement;
     //the 0th item w$$anonymous$$ch is what will display if not$$anonymous$$ng is found
     public string itemType0;
     //first item/event to add to the displayItemsFound text
     public string itemType1;
     //second item/event to add to the displayItemsFound text
     public string itemType2;
     //t$$anonymous$$rd item/event to add to the displayItemsFound text
     public string itemType3;
 
     //t$$anonymous$$s int lets me choose how many times to increment the amount of itemsSpawned, w$$anonymous$$ch will make the function itemTypes run more or less
     public int itemSpawnedNumberToAdd;
 
     void Start () {
 
         //the amount of items spawned will be set to zero
         itemsSpawned = 0;
         //the number of items found on each run of the scene will be chosen here
         numberOfItems = Random.Range(minRange,maxRange);
 
         //if one or more items is found, the itemTypes function is run
         //a positive sound is played
         if (numberOfItems >= 1) {
             textInButton.text = textInButton1;
             displayItemsFound.text += (startStatement);
             itemTypes ();
         }
         //if no items or events occur, the game notifies the player accordingly via the itemType0 string, and the button text is changed as well
         //a negative sound is played
         if (numberOfItems == 0){
             displayItemsFound.text = (itemType0);
             textInButton.text = textInButton0;
             noItems.Play ();
         }
     }
 
     //t$$anonymous$$s function handles w$$anonymous$$ch items (strings) are displayed, and how many
     //remember that t$$anonymous$$s is a text adventure, so I may use strings instead of instantiating physical items via prefabs
     void itemTypes(){
         //if the amount of items spawned is less than the required number of items to be found, throw out a random number in the range
         //the random number will decide w$$anonymous$$ch items are found
         if (itemsSpawned < numberOfItems) {
             itemType = Random.Range (minRange, maxRange);
             items.Play ();
 
             //if the itemType int equals 1, "spawn" the item assigned to itemType1 and add it to the items list
             if (itemType == 1) {
                 itemsFound.Add (itemType1);
                 displayItemsFound.text += (itemType1);
                 //itemType
                 itemsSpawned = itemsSpawned += itemSpawnedNumberToAdd;
                 //itemSpawned++
             }
             //if the itemType int equals 2, "spawn" the item assigned to itemType2 and add it to the items list
             if (itemType == 2) {
                 itemsFound.Add (itemType2);
                 displayItemsFound.text += (itemType2);
                 itemsSpawned = itemsSpawned += itemSpawnedNumberToAdd;
             }
             //if the itemType int equals 3, "spawn" the item assigned to itemType1 and add it to the items list
             if (itemType == 3) {
                 itemsFound.Add (itemType3);
                 displayItemsFound.text += (itemType3);
                 itemsSpawned = itemsSpawned += itemSpawnedNumberToAdd;
             }
             //runs the itemTypes script again, t$$anonymous$$s will loop until the number of items spawned is greater than the amount of items needed
             //the number of itemsSpawned will equal one more than needed, but the function will not run again at t$$anonymous$$s point
             itemTypes ();
         }
     }
 }


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class buttonScript : MonoBehaviour {
 
     public string sceneToLoad;
 
     void OnMouseDown(){
         Application.LoadLevel (sceneToLoad);
     }
 }
 

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

76 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

How i can make a script-made button interactable? 0 Answers

check if list contains item with matching string property 2 Answers

stack by quanity...? C# 2 Answers

Creating a custom List or Collection 1 Answer

Removing item from inventory not updating correct based on order 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