Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Index variable not doing what it is supposed to do.

Discussion in 'Scripting' started by gjaccieczo, Jul 25, 2021.

  1. gjaccieczo

    gjaccieczo

    Joined:
    Jun 30, 2021
    Posts:
    306
    My HeroListScript

    Code (CSharp):
    1.  
    2.     using System.Collections;
    3.     using System.Collections.Generic;
    4.     using System.IO;
    5.     using UnityEngine;
    6.     using Newtonsoft.Json;
    7.  
    8.     public class HeroListScript
    9.     {
    10.         List<HeroConstructor> heroList = new List<HeroConstructor>();
    11.  
    12.  
    13.         void Start()
    14.         {
    15.            ;
    16.         }
    17.  
    18.  
    19.     public class Deserializer
    20.     {
    21.         public static void DeserializingRootItem(List<HeroConstructor> heroList)
    22.         {
    23.             string jsonPath;
    24.             jsonPath = File.ReadAllText(Application.dataPath + "/list_of_heroes.json");
    25.             RootItem deserializedHero = JsonConvert.DeserializeObject<RootItem>(jsonPath);
    26.             HeroListScript heroListScript = new HeroListScript();
    27.             int indexHero1 = 2;
    28.             int heroNumberFromJson = deserializedHero.heroes[indexHero1].heroNumber;
    29.             string heroNameFromJson = deserializedHero.heroes[indexHero1].heroName;
    30.             HeroConstructor hero1 = HeroConstructor(heroNumberFromJson, heroNameFromJson);
    31.             indexHero1 = 5;
    32.             HeroConstructor hero2 = HeroConstructor(heroNumberFromJson, heroNameFromJson);
    33.             heroListScript.heroList.Add(hero1);
    34.             heroListScript.heroList.Add(hero2);
    35.        
    36.             return heroList;
    37.  
    38.         }
    39.  
    40.     }
    41.  
    42.     public void DebugLogTest()
    43.     {
    44.        Debug.Log(heroList[0].heroName);
    45.        Debug.Log(heroList[1].heroName];
    46.     }
    47.  
    48.        public class HeroConstructor
    49.       {
    50.            public int heroNumber;
    51.            public string heroName;
    52.  
    53.  
    54.            public HeroConstructor(int heroNumberFromJson, string heroNameFromJson)
    55.            {
    56.                   heroNumber = heroNumberFromJson;
    57.                   heroName = heroNameFromJson;
    58.            }
    59.        }
    60.  
    61.     [System.Serializable]
    62.     public class RootItem
    63.     {
    64.         [JsonProperty("heroes")]
    65.         public List<Hero> heroes { get; set; }
    66.     }
    67.  
    68.     [System.Serializable]
    69.         public class Hero
    70.         {
    71.             [JsonProperty("heroNumber")]
    72.             public int heroNumber { get; set; }
    73.             [JsonProperty("heroName")]
    74.             public string heroName { get; set; }
    75.  
    76.         }
    77.     }
    78.  
    What i'm trying to do:
    I want indexHero1 (lines 27, 31) to serve as an indexer for the deserializedHero.heroes[indexInt1] and show a different entry on change.

    What is the issue:

    It seems to work just fine, the Debug.Log shows the heroName of the first list entry. However when i change the indexHero1 value to 5 it doesn't seem to react to that at all and continues to show the heroName of the first list entry while it should show a different one.

    The methods are called from another script however i doubt that it has any influence over the problem.

    Thank you for your help.

    Update: apparently i didn't actually set the value to 5. To set the value of indexHero1 you need to create an extra method for that.
     
    Last edited: Jul 25, 2021
  2. gjaccieczo

    gjaccieczo

    Joined:
    Jun 30, 2021
    Posts:
    306
    Resolved the indexHero1 not changing its value.

    HeroListScript:
    Code (CSharp):
    1.  using System.Collections;
    2.     using System.Collections.Generic;
    3.     using System.IO;
    4.     using UnityEngine;
    5.     using Newtonsoft.Json;
    6.     public class HeroListScript
    7.     {
    8.         List<HeroConstructor> heroList = new List<HeroConstructor>();
    9.         void Start()
    10.         {
    11.            ;
    12.         }
    13.     public class Deserializer
    14.     {
    15.         public static void DeserializingRootItem(List<HeroConstructor> heroList)
    16.         {
    17.             string jsonPath;
    18.             jsonPath = File.ReadAllText(Application.dataPath + "/list_of_heroes.json");
    19.             RootItem deserializedHero = JsonConvert.DeserializeObject<RootItem>(jsonPath);
    20.             HeroListScript heroListScript = new HeroListScript();
    21.             int indexHero1 = 0
    22.             int heroNumberFromJson = deserializedHero.heroes[indexHero1].heroNumber;
    23.             string heroNameFromJson = deserializedHero.heroes[indexHero1].heroName;
    24.  
    25.             Debug.Log(indexHero1);
    26.  
    27.             HeroConstructor hero1 = HeroConstructor(heroNumberFromJson, heroNameFromJson);
    28.             IndexHero1Change(ref indexHero1, 1);
    29.             Debug.Log(indexHero1);
    30.  
    31.             IndexHero1Change(ref indexHero1, 2);
    32.             HeroConstructor hero2 = HeroConstructor(heroNumberFromJson, heroNameFromJson);
    33.             Debug.Log(indexHero1);
    34.  
    35.             heroListScript.heroList.Add(hero1);
    36.             heroListScript.heroList.Add(hero2);
    37.      
    38.             return heroList;
    39.         }
    40.     }
    41.     public void DebugLogTest()
    42.     {
    43.        Debug.Log(heroList[0].heroName);
    44.        Debug.Log(heroList[1].heroName];
    45.     }
    46.  
    47.     public static void IndexHero1Change(ref int indexValue, int indexValueToChange)
    48.     {
    49.        indexValue = indexValueToChange;
    50.     }
    51.  
    52.        public class HeroConstructor
    53.       {
    54.            public int heroNumber;
    55.            public string heroName;
    56.            public HeroConstructor(int heroNumberFromJson, string heroNameFromJson)
    57.            {
    58.                   heroNumber = heroNumberFromJson;
    59.                   heroName = heroNameFromJson;
    60.            }
    61.        }
    62.     [System.Serializable]
    63.     public class RootItem
    64.     {
    65.         [JsonProperty("heroes")]
    66.         public List<Hero> heroes { get; set; }
    67.     }
    68.     [System.Serializable]
    69.         public class Hero
    70.         {
    71.             [JsonProperty("heroNumber")]
    72.             public int heroNumber { get; set; }
    73.             [JsonProperty("heroName")]
    74.             public string heroName { get; set; }
    75.         }
    76.     }
    77.  
    An explanation of how it works (and the source for the fix) is:https://stackoverflow.com/questions/14333258/change-value-inside-an-void-extension-method

    However, despite that, my Debug.Log of the heroList.heroName still shows the value of the very first item in the list.
     
  3. gjaccieczo

    gjaccieczo

    Joined:
    Jun 30, 2021
    Posts:
    306
    Any ideas?
     
  4. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    You can foreach your list and see all items in your list.

    foreach(Hero item in herolist){Debug.Log(item.name);} or check how many items you have in your list
     
  5. gjaccieczo

    gjaccieczo

    Joined:
    Jun 30, 2021
    Posts:
    306
    Thank you for your response, Metigosu! The issue is that what i'm trying to do is specifying a particular index for a particular element of the deserializedList using the indeHero1 and even when it's changed to some other value it is still showing up the first entry of the list rather the entry that i want to turn into an object.
     
  6. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    I dont get it from your description maybe someone else will sorry.Maybe get rid of everyting else then your list and try it without other things like serialization etc.
     
    gjaccieczo likes this.
  7. gjaccieczo

    gjaccieczo

    Joined:
    Jun 30, 2021
    Posts:
    306
    No worries, any advice is always appreciated.
     
  8. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,616
    Surely instead of this:
    Code (csharp):
    1.  
    2. int indexHero1 = 2;
    3. int heroNumberFromJson = deserializedHero.heroes[indexHero1].heroNumber;
    4. string heroNameFromJson = deserializedHero.heroes[indexHero1].heroName;
    5. HeroConstructor hero1 = HeroConstructor(heroNumberFromJson, heroNameFromJson);
    6. indexHero1 = 5;
    7. HeroConstructor hero2 = HeroConstructor(heroNumberFromJson, heroNameFromJson);
    8.  
    You meant this:
    Code (csharp):
    1.  
    2. int indexHero1 = 2;
    3. int heroNumberFromJson = deserializedHero.heroes[indexHero1].heroNumber;
    4. string heroNameFromJson = deserializedHero.heroes[indexHero1].heroName;
    5. HeroConstructor hero1 = HeroConstructor(heroNumberFromJson, heroNameFromJson);
    6. indexHero1 = 5;
    7. heroNumberFromJson = deserializedHero.heroes[indexHero1].heroNumber;
    8. heroNameFromJson = deserializedHero.heroes[indexHero1].heroName;
    9. HeroConstructor hero2 = HeroConstructor(heroNumberFromJson, heroNameFromJson);
    10.  
    Otherwise the values of heroNumberFromJson and heroNameFromJson don't change.

    On the other hand, why are you making the code so convoluted? You could just as well write
    Code (csharp):
    1.  
    2. HeroConstructor hero1 = HeroConstructor(
    3. deserializedHero.heroes[2].heroNumber, deserializedHero.heroes[2].heroName);
    4. HeroConstructor hero2 = HeroConstructor(
    5. deserializedHero.heroes[5].heroNumber, deserializedHero.heroes[5].heroName);
    6.  
    or you could write a version of the constructor for HeroConstructor that takes in a deserializedHero as a parameter. Then you could just write
    Code (csharp):
    1.  
    2. HeroConstructor hero1 = HeroConstructor(deserializedHero.heroes[2]);
    3. HeroConstructor hero2 = HeroConstructor(deserializedHero.heroes[5]);
    4.  
     
    Last edited: Jul 26, 2021
    bobisgod234 and gjaccieczo like this.
  9. gjaccieczo

    gjaccieczo

    Joined:
    Jun 30, 2021
    Posts:
    306
    Uhh, that's really stupid of me for sure, haha. For whatever reason i thought i could just put it once in the method and that's it. Thank you, it now works!
    indexInt1 = 2, indexInt1 = 5 didnt work for me, i had to use a separate method for that, see my second post in this thread.

    You'd be surprised but i initially wanted to do indexInt1 as a parameter. https://forum.unity.com/threads/a-n...-that-i-dont-understand.1144562/#post-7351055 Taking in deserializedHero as a parameter was also on my mind but when i tried it out, for whatever reason it ended up in a stack overflow error in Unity so i decided to abandon that idea for a while.
    I realized that i do something wrong late in the works. It didn't come across my mind that you can create a constructor for a constructor. Thank you again very much, i will try this a bit later on!
     
    Last edited: Jul 26, 2021
  10. gjaccieczo

    gjaccieczo

    Joined:
    Jun 30, 2021
    Posts:
    306
    Could you please clarify what you meant by:
    Does it mean creating an extra constructor, a constructor for a constructor, changing the existing one or something else?
     
  11. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,616
    I just mean add an additional constructor. In your current situation, a constructor that accepts deserializedHero would be convenient, but your original constructor is also convenient for when you don't already have a deserializedHero handy. You can have both constructors.

    It's pretty common for a class to have multiple constructors for convenience.
     
    gjaccieczo likes this.
  12. gjaccieczo

    gjaccieczo

    Joined:
    Jun 30, 2021
    Posts:
    306
    Thank you for the clarification! While the original constructor works just fine, it might not be a good solution if i decide to expand the amount of parameters (will cause bad code readability). I might try creating an extra constructor just to make the code clean later on.