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 matthew_gigante · Oct 14, 2017 at 09:17 PM · textlistrandomstringdisplay

Trouble displaying lists of strings in a text box

So I basically have a scene that runs after you hit a search button, and displays a couple of possible items randomly. What I want is for the scene to load, the randomizers to do their thing, and for whatever is found to be added to the text called "itemsFound" in my inspector, relevant code is as follows:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class itemGenerator : MonoBehaviour {
     public Text displayItemsFound;
     //list to display items found
     List<string> itemsFound = new List<string>();
     //how many items will be found
     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;
 
     // Use this for initialization
     void Start () {
         itemsSpawned = 0;
         numberOfItems = Random.Range(0,3);
 
         if (numberOfItems >= 1) {
             itemTypes ();
         }
             
         if (numberOfItems == 0){
             Debug.Log ("You did not find anything");
             displayItemsFound.text = ("You found nothing.");
         //you didn't find anything
         }
     }
 
     void itemTypes(){
         if (itemsSpawned < numberOfItems) {
             itemType = Random.Range (0, 3);
         }
         if (itemType == 1) {
             itemsFound.Add ("Shard of glass.");
             displayItemsFound.text += (itemsFound[0]);
             //itemType
         }
         if (itemType == 2) {
             itemsFound.Add ("Cloth strip.");
             displayItemsFound.text += (itemsFound[1]);
         }
         if (itemType == 3) {
             itemsFound.Add ("A silver coin.");
             displayItemsFound.text += (itemsFound[2]);
         }
     }
     //print out found... (items)
 }


Comment
Add comment · Show 1
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 ghostmode · Oct 14, 2017 at 09:29 PM 0
Share

What is the problem you're having? Is it that only one item displays?

1 Reply

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

Answer by matthew_gigante · Oct 15, 2017 at 01:40 AM

I am getting a few errors with this code, it also says that itemsCollection is never called to

alt text


codework.png (159.8 kB)
Comment
Add comment · Show 2 · 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 ghostmode · Oct 15, 2017 at 02:04 AM 0
Share
 if (!tempItems.Count > 0) {

should be

 if (tempItems.Count > 0) {

and

 numberOfItems-itemsSpawned

should be

 (numberOfItems - itemsSpawned)
avatar image matthew_gigante · Oct 15, 2017 at 02:37 AM 0
Share

I did some work on my own with my original code and came up with a slight adjustment that made it work: I realized that I was never adding anything to itemsSpawned, the integer that made my itemTypes function run again. I also realized i had no real way of making my ItemTypes function run again, so I added itemTypes (); at the bottom of that function.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 

 
 public class itemGenerator : $$anonymous$$onoBehaviour {
     //sounds for finding or not finding items
     public AudioSource items;
     public AudioSource noItems;
 
     public Text displayItemsFound;
     //list to display items found
     public List<string> itemsFound = new List<string>();
     //how many items will be found
     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;
 
     // Use this for initialization
     void Start () {
 
         itemsSpawned = 0;
         numberOfItems = Random.Range(0,3);
 
         if (numberOfItems >= 1) {
             itemTypes ();
         }
         if (numberOfItems == 0){
             displayItemsFound.text = ("You found: nothing.");
             noItems.Play ();
         }
     }
 
     void itemTypes(){
         if (itemsSpawned < numberOfItems) {
             itemType = Random.Range (0, 4);
             items.Play ();
 
             if (itemType == 1) {
                 itemsFound.Add ("Shard of glass.");
                 displayItemsFound.text += (" shard of glass.");
                 //itemType
                 itemsSpawned++;
             }
             if (itemType == 2) {
                 itemsFound.Add ("Cloth strip.");
                 displayItemsFound.text += (" cloth strip.");
                 itemsSpawned++;
             }
             if (itemType == 3) {
                 itemsFound.Add ("A silver coin.");
                 displayItemsFound.text += (" silver coin.");
                 itemsSpawned++;
             }
             itemTypes ();
         }
     }
 }
 
 

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

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

Related Questions

How to Detect Touch on a Random String from a List? 1 Answer

Random role assigning from string array 1 Answer

A node in a childnode? 1 Answer

(c#) display a random string at game over 2 Answers

How do I get a list to randomly select.... 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