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 Serialized Array of Arrays how to access?

Discussion in 'Scripting' started by Xerun1, Nov 14, 2022.

  1. Xerun1

    Xerun1

    Joined:
    Sep 9, 2022
    Posts:
    15
    Edit: please ignore, I figured out a workaround

    I'm not sure I've done this the right way but I can't get any other way to work. I want to create an array of arrays such that I can go into the Editor and for each prefab enemy say I want 3 arrays, and then input the values for each of the 3 arrays. I've managed to get this part working using scriptable objects using the following code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [CreateAssetMenu(fileName = "StatDictionaryEnemy", menuName ="Enemy_Stats/NewStats", order =0)]
    6. public class StatDictionaryEnemy: ScriptableObject
    7. {
    8.     [SerializeField] CharacterClass[] Character_Type = null;
    9.  
    10.  
    11.  
    12.  
    13.     [System.Serializable]
    14.     class CharacterClass
    15.     {
    16.         [SerializeField] public EnemyClassType ClassTypeAssigned;
    17.    
    18.           public EnemyAIPhases[] NumPhases;
    19.  
    20.     }
    21.  
    22.  
    23. Edit: please ignore I figured it out
    24.  
    25.  
    26.        [System.Serializable]
    27.     public class EnemyAIPhases
    28.     {
    29.         public float[] AttackWeights;
    30.     }
    31. }

    The problem I've now run into is I cant access the arrays because they are of type EnemyAIPhases and no other script can figure out what that class is.

    I just want to be able to access the entire array underneath by accessing the index of the first array
     
    Last edited: Nov 14, 2022
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    Seems like you figured it out, but you need to make your classes
    public
    , not private.
     
  3. Xerun1

    Xerun1

    Joined:
    Sep 9, 2022
    Posts:
    15
    Are they not? I have public against all of them?


    Incase anyone else needs this answer I just created a Dictionary by looping through them and assigning that an Integer value. Not sure its the best way but it seems to work fine for what I'm doing:
    Code (CSharp):
    1. void BuildAttackTable()
    2.         {
    3.            PhaseCount =  new Dictionary<EnemyClassType, int>();
    4.            AttackTable = new Dictionary<EnemyClassType, Dictionary<int, float[]>>();
    5.            int i = 0;
    6.             foreach(CharacterClass ECCLU in Character_Type)
    7.             {var AttackWeightLookuptable = new Dictionary<int, float[]>();
    8.             foreach(EnemyAIPhases AI in ECCLU.NumPhases)
    9.             {
    10.                 AttackWeightLookuptable[i] = AI.AttackWeights;
    11.                 i=i+1;
    12.             }AttackTable[ECCLU.ClassTypeAssigned] = AttackWeightLookuptable;
    13.             PhaseCount[ECCLU.ClassTypeAssigned] = i;
    14.          
    15.           }
    16.         }
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    Not
    CharacterClass
    which you haven't specified an access modifier for, meaning it's private, meaning nothing outside
    StatDictionaryEnemy
    knows that it exists.
     
  5. Xerun1

    Xerun1

    Joined:
    Sep 9, 2022
    Posts:
    15
    Ah right! thank you