Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How do i display all my arrays in string? It only shows one.

Discussion in 'Scripting' started by asiangamemaker, Jul 31, 2016.

  1. asiangamemaker

    asiangamemaker

    Joined:
    May 17, 2015
    Posts:
    25
    Hello. I wanted to try to display my array prices in the game using the Example: showPrices.text ethod. It works but it only shows 1 of my arraylist. May i know how am i able to show all? My console seems to have printed the arrays. I want the same to be display as my in game. I have attached a pic so you guys can look at the problem clearly.

    Here is my code

    public class IngredientShop : MonoBehaviour
    {
    public string[] Ingredient = newstring[6];
    public float[] Prices = newfloat[6];
    public Text shopPrices;
    inti=0;
    //Usethisfor initialization
    private void Start()
    {
    for(i=0;i<Prices.Length;i++)
    {
    Prices[0]=1.20f;
    Prices[1]=0.90f;
    Prices[2]=1.80f;
    Prices[3]=1.60f;
    Prices[4]=1.30f;
    shopPrices.text=Ingredient+"\t"+Prices;
    Debug.Log(Ingredient+"\t"+Prices);
    }
    }
    prata error.png
     
  2. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    have you set up the Text so it has Vertical overflow?
     
  3. asiangamemaker

    asiangamemaker

    Joined:
    May 17, 2015
    Posts:
    25
    Hi, yes i just changed it. No difference. No changes.
     
  4. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    what about replacing \t with \n?

    Also you should use code tags when posing, it's easier to read :p
     
  5. asiangamemaker

    asiangamemaker

    Joined:
    May 17, 2015
    Posts:
    25
    i also did change the \t with \n . no difference hahahaha. oh my. Idk why but i hate tags hahah. The fact that im unsure on how to use it on this arrays hahaha. Right now, i need to know how to get the display working haha
     
  6. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    Try changing
    Code (CSharp):
    1. shopPrices.text=Ingredient+"\t"+Prices;
    To

    Code (CSharp):
    1. shopPrices.text=Ingredient+"\t"+Prices[i];
    and weird, I just tried using \n and it worked in unity 5.3.1 :O
     
  7. asiangamemaker

    asiangamemaker

    Joined:
    May 17, 2015
    Posts:
    25
    im using 5.3.5f hahaha
     
  8. asiangamemaker

    asiangamemaker

    Joined:
    May 17, 2015
    Posts:
    25
    Btw, did what you said. There's no difference. Is it cool if i see yours?
     
  9. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    Sorry I've just headed out and am away from computer, however a quick but inefficient fix is removing the forloop completely and using below

    Code (CSharp):
    1. shopPrices.text =  Prices[0].ToString() + "\n" + Prices[1].ToString() + "\n" + Prices[2].ToString() + "\n"
    2.             + Prices[3].ToString() + "\n" + Prices[4].ToString() + "\n" + Prices[5].ToString() + "\n";
     
  10. asiangamemaker

    asiangamemaker

    Joined:
    May 17, 2015
    Posts:
    25
    Okay that solves the problem. However, i hope to get the script with the forloop working :/ If you know the efficient fix, hope you can share it with me :) Thanks for you time mate!
     
  11. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    Soon as i'm back at my place will slap the other one on here! :)
     
  12. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    you could try something like this
    Code (CSharp):
    1. string tempString = "";
    2. for (int i = 0; i < Prices.Length; i++) {
    3.     tempString += string.Format("{0}\n", Prices[i]);
    4. }
    5. shopPrices.text = tempString;
    6.  
     
  13. asiangamemaker

    asiangamemaker

    Joined:
    May 17, 2015
    Posts:
    25
    ITS WORKING!!! THANKS PASSER! one note, may i know what does {0} means?
     
  14. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    the "{0}" is a placeholder for string.Format() to use

    the first argument of string.Format(), is the string you are formatting, and the 2nd argument is what to replace {0} with. If i had a {1}, {2} etc i would be able to provide more arguments that it can replace with.

    this is just my preference since i find it is cleaner than adding strings with +, and that it automatically calls .ToString() on anything you pass in.

    Code (CSharp):
    1. tempString += "/n" + Prices[i];
    would be the same result