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

Question How to instantiate elements of list from different class as gameobject?

Discussion in 'Scripting' started by Jamryl, Mar 12, 2021.

  1. Jamryl

    Jamryl

    Joined:
    Mar 9, 2020
    Posts:
    25
    Good evening lads.

    I need help to get all the traits from a list from another class and instantiate them as gameobjects then set them a specific image and show them to the player.

    I've got a class called "Npc", wich represents instance of auto generated instanciate NPCs containing their name stats, traits, skills etc...

    Class is in shorter something like that:
    Code (CSharp):
    1.  
    2. [Serializable]
    3. public class Npc
    4. {
    5. public string NameLocalizationKey;
    6. public int Height;
    7. public int Weight;
    8. public string EyeColor;
    9. public string HairColor;
    10. public string Haircut;
    11. public List<Trait> Traits;
    12. }
    I would like to show the traits proper to the selected NPC.
    Note that the instantiate NPC is "Selected.Npc"

    I reckon that I need first to load the selected NPC trait list, get one trait, instantiate it as GameObject then change it's sprite to the adequate one. And redo.

    How I am suppose to do that?:(

    my trait class is as bellow:

    Code (CSharp):
    1.  
    2. [Serializable]
    3.     public class Trait
    4.     {
    5.         public string NameLocalizationKey;
    6.         public string DescriptionLocalizationKey;
    7.         public string[] IncompatibleTraitLocalizationKey;
    8.         public StatName Stat1AffectedName;
    9.         public int Stat1AffectedValue;
    10.         public TraitStatEffectUnit Stat1AffectedUnit;
    11.         public StatName Stat2AffectedName;
    12.         public int Stat2AffectedValue;
    13.         public TraitStatEffectUnit Stat2AffectedUnit;
    14.         public TraitCondition Condition;
    15.         public bool StartingTrait;
    16.         public bool IsNegative;
    17.         public bool AffectStat;
    18. }
    NameLocalizationKey is always like that: trait_name_trait1, trait_name_trait2 etc.. and value are stored into a GameData.json

    And my traitAsset class is like that:

    Code (CSharp):
    1. public class TraitAssets : MonoBehaviour
    2. {
    3.     public static TraitAssets Instance { get; private set; }
    4.     private void Awake()
    5.     {
    6.         Instance = this;
    7.     }
    8.     public Sprite spendthriftySprite;
    9.     public Sprite thriftySprite;
    10.     public Sprite studiousSprite;
    11.     public Sprite lazySprite;
    12. }
     
  2. DaRealXDev

    DaRealXDev

    Joined:
    Feb 5, 2021
    Posts:
    13
    What exactly are you trying to do? Do you just wanna create a new game object for every trait in the Traits list and change it to a specific sprite????
     
  3. Jamryl

    Jamryl

    Joined:
    Mar 9, 2020
    Posts:
    25
    Good morning Xavier.

    In my scene I already have in my UI a traitSlotContainer with a grid layout component and as a child traitSlotTemplate a simple image.
    I would like that each trait of the list Traits of the class Npc to be change into the traitSlotTemplate and have its image change to the proper one.

    Sorry for my unclear formulation and thanks for your answer.
     
  4. Jamryl

    Jamryl

    Joined:
    Mar 9, 2020
    Posts:
    25
    Bump!