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

Question ArrayList not visible in Inspector

Discussion in 'Scripting' started by HissyGaming, Mar 11, 2023.

  1. HissyGaming

    HissyGaming

    Joined:
    May 15, 2022
    Posts:
    3
    I made a ArrayList, and the code looks like this:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ArrayListTest : MonoBehaviour
    6. {
    7.     public ArrayList testArrayList = new ArrayList();
    8. }
    I want it to show up in the Inspector, so I made it public, yet it didn't work so how do I make it show up?
    There are no errors in this script at all.
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    ArrayLists can't be Serialized by Unity because they can hold literally any random object. What are you trying to put in it?
     
  3. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    437
  4. HissyGaming

    HissyGaming

    Joined:
    May 15, 2022
    Posts:
    3
    I'm trying to put other scripts into that ArrayList, but they are different classes, so a list won't work as it only lets one type of data in the list. (This is a reply to RadRedPanda)
     
  5. HissyGaming

    HissyGaming

    Joined:
    May 15, 2022
    Posts:
    3
    Ok I will use List<T>
     
  6. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    437
    I see no programming reason to push all scripts into one object. Can you explain the purpose of this action, what do you want to achieve?
     
  7. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,184
    You can use the base type the scripts derive from.
    Code (csharp):
    1. public List<MonoBehaviour> scripts;
    From there you can check what the type is.
    Code (csharp):
    1. if (scripts[index] is Foo)
    And access methods and fields by casting it.
    Code (csharp):
    1. (scripts[index] as Foo).MyField = 0;
    2. (scripts[index] as Foo).MyMethod();
     
    Last edited: Mar 12, 2023
    AngryProgrammer likes this.