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

question about my for loop/best practices question for situation.

Discussion in 'Scripting' started by Sylvir, Jan 28, 2016.

  1. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    hey everyone,

    So I am trying to use a for loop to iterate through the list of my skills in this game. I then want to use that to display a list of all the skills in my game on the character creation screen. I am wanting to use the unity UI system to do this.

    What i have tried to do was to place a new ui text object into my scene and then named it


    here is a copy of the code i am using to try and get this behavior. (the general idea is it will show the skill name then i will do a simlar thing to have another text ui object show the values of the skills as the points are addjusted and stuff. my question is 2 fold. first why is this not showing a list of my skills? (when i did this in the old OnGUI function it worked and showed the list of allt he skills as it iterated through them and gave the name. 2nd is what would be the best practices to use when trying to get this behavior? what I am trying to do, or something else?

    CODE :
    Code (CSharp):
    1. for (int cnt = 0; cnt < Enum.GetValues(typeof(SkillName)).Length; cnt++)
    2.         {
    3.             skillNameTxt.text = ("" + (SkillName)cnt).ToString();
    4. }

    if anyone has any ideas about why this does not work this way, but does when i do it in the old OnGUI with the label then the new rect (with the placement of it) then the same + cancanted on the end or if you know of a way that i can make this work that would be awesome, thanks!
     
  2. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    this function is also called in the update function so it should not just stop frogot to add that part
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You need to create a new GameObject for each text element. The typical way to do this is as a prefab.
     
  4. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    669
    Why that? If only a text should be displayed than there is no need to use different gameObjects.

    @Sylvir
    I guess you are only seeing the last skill, right?
    That is because you are always setting the text to show completely new and not appending. One way to achieve this would be the following:

    Code (CSharp):
    1.  
    2. skillNameTxt.text += (""+(SkillName)cnt).ToString();
    (Note that I did not test your code and can't say for sure if the mapping of the Enum works here as intended

    However you should never ever do that every frame, in update, OnGUI or a for loop. Instead just set the text once at game start (in Start() ).

    I personally would go for a StringBuilder to build the output first and then get the string to shpw, just because it is better performance wise and does not produce as much garbage. In that case your stuff will look like this with some additional optimization:

    Code (CSharp):
    1.  
    2. var outputStringBuilder = new StringBuilder();
    3. foreach (var enumValue in Enum.GetValues(typeof(testEnum)))
    4. {
    5.     outputStringBuilder.AppendLine(enumValue.ToString());
    6. }
    7.  
    8. skillNameTxt.text = outputStringBuilder.ToString();
    9.  
     
  5. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    The old OnGUI functionality was an immediate mode UI, which means you called functions every frame to instruct unity what your GUI should look like. The new GUI is a retained mode UI. This means you create game objects to represent the UI and you set properties on them and they persist. Unless something is changing you don't have to loop through everything every frame and set the values. You just have to create the objects either in the editor or at runtime and set the properties. Unity takes care of displaying everything.
     
    Sylvir and Kiwasi like this.
  6. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    cool, thank you everyone for your responces. I will use what you suggested and work from there :) appreciate it. the reason its not working now is making more sense to me now
     
  7. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    @Glockenbeat adding the += resaulted in the names of all the skills being listed but it then repeated indefinatly untill there was no more room left.. I am going to try to work around with what you said next to try...

    @BoredMormon could you explain to me why i would need to have a specific game object for each item when all it is that i need to be displayed is the text that has each one listed? I could understand if i needed to do more with each one then display it, but as it just needs to list all the skills i am not understanding where i would need to do this. I am not doubting that i would need to as you have helped me and many others on here with many issues and questions. I am just asking for a bit of explaination on this.

    I know that i could simply write out the names of each of the skills then just make a function append the skills value at the end but i would like to try and work out a solution to this so that if i decide to add a couple more skills in at a later date that it will just take those into consideration and add them as well instead of having to go back in and add a new line for each new skill and append the value to it individually.

    thanks again for both the responses. I appreciate your time.
     
  8. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    I forgot to thank you as well I appreciate you taking the time to explain the differences there for me. Very helpful to know some of the differences in how they both work.
     
    Dave-Carlile likes this.
  9. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    @Glockenbeat using the stringbuilder set up you mentioned there it seems to be working quiet perfectly, but it is returning the enumeration value. so it is just returning back a 0 then 1 2 3 and so on for each of the skills. witch i undestand is the enumeration value for that skill.. I am looking up the string outputbuilder documentation to see what i can do to call the actual name that is in the place of each value in the enumeration. (the skill value itself is another place and will be recalled using this same method later and be set next to it so that as they adjust their skill points up and down it shows where that would have an effect on the skills. aka put strength up a couple points then look and can see how the melle attack skill went up or something to that effect). Thanks for pointing me in the right direction.