C# script error Cannot apply indexing with [] to an expression of type GameObject

I’m trying to convert one of my javascripts to C#. When I add the following line to my C# script it generates the error below:

bullet[activeBullet].gameObject.active = true;

Since I’m new to C#, I’m not sure how to deal with getting that to work. Any ideas? Here’s what the whole script looks like:

public class Enemy : MonoBehaviour {
	public GameObject bullet;
	public int bulletCache = 2;
	int activeBullet = 0;
	
	void FireBullet() {
		bullet[activeBullet].gameObject.active = true;
		activeBullet+=1;
		
		if(activeBullet > bulletCache-1){
			activeBullet = 0;//reset the loop
		}
	}
}

you define bullet as a gameObject in there, that makes it rather clear that you can not access it as if it were an array because you didn’t define it as one nor initialize it as one.

perhaps you wanted to use

public class Enemy : MonoBehaviour {
	public GameObject[] bullet = new GameObject[1];
	public int bulletCache = 2;
	int activeBullet = 0;
	
	void FireBullet() {
		bullet[activeBullet] = new GameObject();
		bullet[activeBullet].gameObject.active = true;
		activeBullet+=1;
		
		if(activeBullet > bulletCache-1){
			activeBullet = 0;//reset the loop
		}
	}
}
3 Likes

Oh wow how did I miss that? Thanks for the fast reply!

1 Like

using UnityEngine;
using System.Collections;
public class PathFollower : MonoBehaviour {
public Transform target ;
public float speed;
public int current;

void Update () {
if (transform.position != target[current].position) {
Vector3 pos = Vector3.MoveTowards(transform.position,target[current].position,speed * Time.deltaTime);
GetComponent().MovePosition(pos);
} else
current = (current + 1) % target.Length;

}
}
Kindly guide me how to solve this problem.
Best Regards

using UnityEngine;
using System.Collections;
public class PathFollower : MonoBehaviour {
public Transform target ;
public float speed;
public int current;

void Update () {
if (transform.position != target[current].position) {
Vector3 pos = Vector3.MoveTowards(transform.position,target[current].position,speed * Time.deltaTime);
GetComponent().MovePosition(pos);
} else
current = (current + 1) % target.Length;

}
}
Kindly guide me how to solve this problem.
Best Regards

Firstly, please dont post twice.
Secondly, use code tags:
https://discussions.unity.com/t/481379
Thirdly please post your error aswell.
Fourthly, please make your own thread instead of trying to revive this one.

Fifthly – you’re trying to index a singular variable. target is not an array, so you cannot index it.
You want : public Transform [] target; // or maybe "targets" ;)

1 Like

pls can someone help me

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

public class ObjectPooler : MonoBehaviour
{
    public GameObject pooledObject;
  
    public int pooledAmount;

    List<GameObject> pooledObjects;

    void Start()
    {
        pooledObjects = new List<GameObject>();
        for (int i = 0; i < pooledAmount; i++)
        {
            GameObject obj = (GameObject)Instantiate(pooledObject);
            obj.SetActive(false);
            pooledObjects.Add(obj);
        }
    }

    public GameObject GetPooledObject()
    {
        for( int i =0; i < pooledAmount; i++)
        {
            if (!pooledObjects[i].activeInHierarchy)
            {
                return pooledObject[i];
            }
        }


        GameObject obj = (GameObject)Instantiate(pooledObject);
        obj.SetActive(false);
        pooledObjects.Add(obj);
        return obj;



    }



  


}

i have linked the file.I am getting this error
on line 31 which is "return pooled*; "*
“Cannot apply indexing with [ ] to an expression of type ‘GameObject’”

4686707–441875–ObjectPooler.cs (971 Bytes)

the answer has already been given, you just need to implement it in your own script, which has more flaws than only that error. Just try this one:

using System.Collections.Generic;
using UnityEngine;

public class MyScript : MonoBehaviour
{
    public GameObject pooledObject;

    public int pooledAmount;

    List<GameObject> pooledObjects = new List<GameObject>();

    void Start()
    {
        for (int i = 0; i < pooledAmount; i++)
        {
            GameObject obj = Instantiate(pooledObject);
            obj.SetActive(false);
            pooledObjects.Add(obj);
        }
    }

    public GameObject GetPooledObject()
    {
        for (int i = 0; i < pooledAmount; i++)
        {
            if (!pooledObjects[i].activeInHierarchy)
            {
                return pooledObjects[i];
            }
        }

        return default;
    }

    // usage
    void SpawnFromPool()
    {
        GameObject go = GetPooledObject();

        if (go != default(GameObject))
        {
            // Set objects new position, rotation, whatever...

            go.SetActive(true);
        }
    }
}