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
}
}
}
using UnityEngine;
using System.Collections;
public class PathFollower : MonoBehaviour {
public Transform target ;
public float speed;
public int current;
using UnityEngine;
using System.Collections;
public class PathFollower : MonoBehaviour {
public Transform target ;
public float speed;
public int current;
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" ;)
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’”
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);
}
}
}