简体   繁体   中英

Creating a list of <T> class on an object

This is a common question and I assure you I have done my research first. I simply cannot get a list of all of the instances of a script type on the game object.

I have tried making an array of the types and looping the contents into a list. This gives me conversion errors.

I have tried directly adding the array to the list with .AddRange. Conversion errors.

I have tried the different formats of GetComponents, and casting the output of the array into every applicable type I can think of, with no success.

I have also tried initising the list first and then running GetComponent in start.

I have tried using CharEquipGenre as both monobehaviour and non-monobehaviour.

What am I doing wrong?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;

public class CharEquipment : MonoBehaviour
{   
public List<CharEquipGenre> equipment_genres = GetComponents <CharEquipGenre>(); // I am trying to do something like this
public CharEquipGenre attack;
public CharEquipGenre defend;
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharEquipGenre
{
public List<BlockScriptableObject> equipped = new List<BlockScriptableObject>();
}

// Additional code via request:

public class CharEquipment : MonoBehaviour
{

void Start()
{
    equipment_genres = GetComponents<CharEquipGenre>();
}

public List<CharEquipGenre> equipment_genres = new System.Collections.Generic.List<CharEquipGenre>();

public CharEquipGenre attack;
public CharEquipGenre defend;

Well, CharEquipGenre is not a MonoBehaviour, so there are no "components" of this class.

You can find all components of a specific type with GetComponents (read more here ) but it needs to be a MonoBehaviour that's attached to the GameObject

I don't know unity3d but it seems as though you are trying to initialize equipment_genres when defining it and for that the compiler would need access to something that is available at compile time.

If GetComponents is a method on the class then this will not work as the method is not static. You could use the instance by perhaps going with a method or expression body:

    public List<CharEquipGenre> equipment_genres => new List<CharEquipGenre>(GetComponents<CharEquipGenre>());

Although a slightly "better" design would be exposing IEnumerable :

    public IEnumerable<CharEquipGenre> EquipmentGenres => ...
    // or
    public IEnumerable<CharEquipGenre> GetEquipmentGenres() => ...

First of all make your class [Serializable] so you can see it in the inspector and save it.

[Serializable]
public class CharEquipGenre
{
    public List<BlockScriptableObject> equipped = new List<BlockScriptableObject>();
}

And then simply instantiate a new list like

public List<CharEquipGenre> equipment_genres = new List<CharEquipGenre>();

Note that both of those will be overruled by whatever you do to them in the Inspector later. Once you added elements any change to the initialization lines is useless (unless you hit Reset in the MonoBehaviour 's context menu)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM