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. Dismiss Notice

Checking what an Element in an Array contains.

Discussion in 'Scripting' started by heinickesg, Jun 22, 2014.

  1. heinickesg

    heinickesg

    Joined:
    Jan 25, 2013
    Posts:
    48
    Hi All!

    Ok Im having trouble finding how to do this, So I have an array that is always 5 long and is always in the same order.

    So the Array is called "character info" and gets the following values:

    Element 0 gender
    Element 1 race
    Element 2 class
    Element 3 level
    Element 4 xp

    I want to check each element in the array, For example:

    Code (csharp):
    1. if(element0 == female)
    2. {
    3. //load the female model
    4. }
    im sure this is rather easy... But I can not seem to get it working.
     
  2. paranoidx

    paranoidx

    Joined:
    Apr 17, 2014
    Posts:
    43
    Code (csharp):
    1.  
    2. string[] charInfo = new string[5];
    3.  
    4. //to assign
    5. charInfo[0] = "female";
    6.  
    7. //to retrieve gender
    8. if(charInfo[0] == "female"){
    9.     //load female model
    10. }
    11.  
    however I suggest you look into making a class, as those numbers will get lost in translation as you code further down the track.
     
  3. heinickesg

    heinickesg

    Joined:
    Jan 25, 2013
    Posts:
    48
    Its not working.... Here is what I have:

    Code (csharp):
    1. public string[] characterInfo;
    Code (csharp):
    1. void useCharacterInfo()
    2.     {
    3.         if(characterInfo[0] == "Female")
    4.         {
    5.             Debug.Log ("Female!");
    6.         }
    7.  
    8.     }
    9.  
    10.     // Update is called once per frame
    11.     void Update ()
    12.     {
    13.         useCharacterInfo();
    14.     }
    So as I said I have it assigned in my script already, I should be getting spammed with Female but i'm not...
     
  4. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Are you sure female is in the index 0? Test it,

    Check if(characterInfo[0] != "Female") and see what the outcome is..

    Also it's case sensitive, so if you've written "female" that' not the same as "Female"
     
  5. Erisat

    Erisat

    Joined:
    Jan 31, 2013
    Posts:
    88
    is that all you have? because you seem to be checking an element that hasnt been assigned. at some point you would have to be like characterInfo[0] = "Female"; to assign that element to actually contain something. the answer paranoidx gave should work just fine.

    edit: i see you said that you are assigning it. maybe giving more of the script you have will help.
     
  6. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    not to hurt your feelings ;-) but this is real bad coding, you should take paranoidx' advice and make a class containing these things. typically you would also use an enum for things like race, class,... i'd even go for gender.
    example:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Character: MonoBehaviour
    5. {
    6.     public Race race;
    7.     public Class klass;//class is acctualy a bad word to use since ist a key word
    8.     public Gender gender;
    9.     public int level;
    10.     public float xp;
    11.     ...
    12. }
    13. public enum Race
    14. {
    15.     elf,
    16.     dwarf,
    17.     gaint,
    18.     unicorn
    19. }
    20. public enum Class
    21. {
    22.     tank,
    23.     assasin,
    24.     swordsman
    25. }
    26. public enum Gender
    27. {
    28.     male,
    29.     female,
    30.     unisex,
    31.     genderless
    32. }
     
    Magiichan likes this.
  7. heinickesg

    heinickesg

    Joined:
    Jan 25, 2013
    Posts:
    48
    To sum up everyone above, these values are assigned elsewhere in the script. I can't use the class idea because these values are read of a website database.

    I did guess it was case Sensitive, I made sure the cases were the same, and yes When in play mode element 0 starts null, then gets assigned "Female"

    Which is why I placed the code in the update function to constantly check if the value is true.
     
  8. heinickesg

    heinickesg

    Joined:
    Jan 25, 2013
    Posts:
    48
    Setting it to != spams my console with "Female" but when female is in element 0 it does not stop -_- this is lame.
     
  9. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    what stops you from assigning these value's to a class it's properties? you could even give the class a function to read in the website database values itself...
    edit: to help you on topic, try printing out the array. see if the elements are at their right place and spelled correct?
     
  10. heinickesg

    heinickesg

    Joined:
    Jan 25, 2013
    Posts:
    48
    They are, I can see them in the inspector because the array is public.
     
  11. heinickesg

    heinickesg

    Joined:
    Jan 25, 2013
    Posts:
    48
    Code (csharp):
    1. void useCharacterInfo()
    2.     {
    3.         if(characterInfo[0] == "Female")
    4.         {
    5.             Debug.Log ("Female!");
    6.         }
    7.         if(characterInfo[0] == "Male")
    8.         {
    9.             Debug.Log ("Male!");
    10.         }
    11.         if(characterInfo[0] == "")
    12.         {
    13.             Debug.Log ("Element 0 is null");
    14.         }
    Ok I made this code, the code is run when you press a button It prints out "element 0 is null" ONCE. and no more, regardless of how many times I press the button


    EDIT:

    I see some people saying to use Lists instead of arrays... could that be the issue?
     
  12. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    just do
    Code (CSharp):
    1. foreach(string s in characterInfo)
    2. {
    3.     Debug.Log(s);
    4. }
     
  13. heinickesg

    heinickesg

    Joined:
    Jan 25, 2013
    Posts:
    48
    it Prints:

    Female
    Human
    Ranger
    1
    0

    So gender, race, class, level, xp as expected.

    this also changes when the elements in the array are changed. (from female to male, or not printing anything when nothing is selected)
     
  14. heinickesg

    heinickesg

    Joined:
    Jan 25, 2013
    Posts:
    48
    I added your script to the button i have, I wonder if its checking it before there is a value (as there is a delay getting the info from the web)
     
  15. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    then try
    Code (CSharp):
    1. foreach(string s in characterInfo)
    2. {
    3.     if(s == "Female")
    4.         Debug.Log(s);
    5. }
    also how are you getting the info?
     
  16. heinickesg

    heinickesg

    Joined:
    Jan 25, 2013
    Posts:
    48
    That code did not work, Here is where the info is coming from:

    Code (csharp):
    1. public IEnumerator checkCharacterInfo(string user, string charName)
    2.     {
    3.         string recieveCharacterInfo = getPlayerCharInfoURL + "user=" + user + "&charName=" + charName;
    4.  
    5.         WWW characterInfo_get = new WWW(recieveCharacterInfo);
    6.         yield return characterInfo_get;
    7.  
    8.         recievedCharacterInfo = characterInfo_get.text;
    9.         characterInfo = recievedCharacterInfo.Split("\n"[0]); //assing the text from the webpage into the array "characterInfo"
    10.         characterInfoLoaded = true;
    11.  
    12.  
    13.     }
     
  17. heinickesg

    heinickesg

    Joined:
    Jan 25, 2013
    Posts:
    48
    Code (csharp):
    1. void useCharacterInfo()
    2.     {
    3.             if(characterInfo[0] == "Female")
    4.             {
    5.                 Debug.Log ("Female!");
    6.             }
    7.             if(characterInfo[0] == "Male")
    8.             {
    9.                 Debug.Log ("Male!");
    10.             }
    11.             if(characterInfo[0] == "")
    12.             {
    13.                 Debug.Log ("Element 0 is null");
    14.             }
    15.     }
    I placed this code in the update function... Interestingly it spams the console with "element 0 is null" When It gets entered either Male or Female it stops printing that but does not print the other lines.

    EDIT:

    What?! If I enter the values manually, it works perfectly.... there muyst be a string error
     
  18. heinickesg

    heinickesg

    Joined:
    Jan 25, 2013
    Posts:
    48
    The string is getting an extra space at the end, That is the issue. Thank you all for the help!