when trying to add a GameObject to a list - CS1001: Identifier expected

I am still fairly new to C# but,
I cannot figure out what my error is here. I am trying to add the GameObject that entered the collider to a list of GameObjects. initial thought would have been that maybe the ‘other’ is a collider or transform of sorts but I still don’t think that makes sense but to be honest I am not sure. Any help much appreciated!
(heres the whole code I have at the moment)

public class craftingScript : MonoBehaviour
{

    [SerializeField] private string pickuppableTag = "pickuppable";

    public GameObject leaf, stick, stone;

    public GameObject[] items;

    public GameObject[] r_string = leaf;

    void Update()
    {
       
    }

    void OnCollisionEnter(Collider other)
    {
        if(other.transform.CompareTag(pickuppableTag))
        {
            items[] = new GameObject(other);
        }
    }
}

Maybe im completely blind today, but i don’t see a List anywhere. Just some arrays.

Additionally, depending on your approach, OnCollision and OnTrigger are considerably different, and may require you to look at your use of rigidbody, or lack of.

private List<GameObject> objectsList = new List<GameObject>();

private void AddGameObjectToList(GameObject _gameObject)
{
    objectsList.Add(_gameObject);
}
1 Like
items[] = new GameObject(other);

This isn’t valid syntax.

You need to provide an index to assign to a position in an array:

items[0] = other.gameObject;

However you should use a List<T>, rather than an array.

2 Likes

I thought GameObject[ ] was a list… It isn’t an array because throughout the code the length will change depending on how many items are inside the collider

No… it’s an array.

// this is an array
private GameObject[] gameObjectArray;

// this is a list
private List<GameObject> gameObjectList;

Google ‘C# array’ and ‘C# list’ and familiarise yourself with the differences.

3 Likes