- Home /
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)
}
What is the problem you're having? Is it that only one item displays?
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
if (!tempItems.Count > 0) {
should be
if (tempItems.Count > 0) {
and
numberOfItems-itemsSpawned
should be
(numberOfItems - itemsSpawned)
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
Follow this Question
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