[Solved] Problem with adding GameObject positions to Vector3 array

Hi,

It might be a simple question but how do I add GameObjects their position to a Vector3 array.

I tried doing:

            for (int i = 0; i < SpawnCubes.Length; i++)
            {
                //Sets spawnpositions to all the right values
                SpawnPositions[i] = SpawnCubes[i].transform.position;
            }

But it didn’t work :confused:

Hope you can help!

what happens?

Do you get an error?

NullReferenceException: Object not reference not set to instance of an object.

Here you have the full script:

public class GameEvent_SpawnShootable : MonoBehaviour {

        private GameEvents_Master gameEventsMaster;

        public float minTimeBetweenSpawn = 0.5f;
        public float maxTimeBetweenSpawn = 2f;

        private float LastSpawnTime;
        private float SpawnRate;
        private float nextSpawn;

        private Vector3[] SpawnPositions;

        public GameObject[] SpawnCubes;

        public GameObject ShootableObject;

        void Start ()
        {
   
        }
   
        void Update ()
        {
            if(Input.GetKeyDown(KeyCode.A))
            {
                gameEventsMaster.CallEventSpawnCube();
            }
        }

        void OnEnable()
        {
            SetInitialReferences();
            gameEventsMaster.EventSpawnCube += SpawnCube;
        }

        void OnDisable()
        {
            gameEventsMaster.EventSpawnCube += SpawnCube;
        }

        void SetInitialReferences()
        {
            gameEventsMaster = GetComponent<GameEvents_Master>();
            SpawnCubes = GameObject.FindGameObjectsWithTag("CubeSpawn");

            for (int i = 0; i < SpawnCubes.Length; i++)
            {
                //Sets spawnpositions to all the right values
                SpawnPositions[i] = SpawnCubes[i].transform.position;
            }
        }

        void SpawnCube()
        {
            int RandomCount = Random.Range(0, SpawnPositions.Length);
            Instantiate(ShootableObject, SpawnPositions[RandomCount], Quaternion.identity);   
        }
    }

Ok so based on my quick glimpse, maybe there isn’t a tag in the gameobjects that you’re searching for. Thereby, leaving the gameobjects array empty.

it says it’s on line 55

I don’t have much else, i just started. I can show you the Master script if you want:

    public class GameEvents_Master : MonoBehaviour {

        public delegate void GameEventHandler();

        public event GameEventHandler EventSpawnCube;

        public void CallEventSpawnCube()
        {
            if(EventSpawnCube != null)
            {
                EventSpawnCube();
            }
        }
    }

I have the tag “SpawnObject” attached to 9 gameObjects. It puts them in the array. That i can see in the inspector. It’s really weird

Ah sorry. Even the error I can’t decipher. Wait till someone who knows more than me come around.

No problem. Thanks for trying! :slight_smile:

try to manually put the gameobject and see how that works
remove the SpawnCubes =GameObject.FindGameObjectsWithTag(“CubeSpawn”);
and manually drag and drop the gameobject to the script

There is no way the line of code

        private Vector3[] SpawnPositions;

will find out how many elemets without you declaring it, so it’s set to 0 initially.

either as a public variable or like

[SerializeField]private Vector3[] SpawnPositions; // [SerializeField] makes private variables viewable in inspector, but can't be editied it's just to check if the values are getting stored.
public GameObject[] SpawnCubes[]

public void Start()
{
  SpawnPositions = new vector3[SpawnCubes.length]; // declares size of SpawnPositions array to same size as SpawnCubes
}

This will set it to be the same size as the game objects, and the other code should work :slight_smile:

Thank you so much! Thanks to all you guys. U da real MVP

Could you change the title to solved, it’s misleading

Welcome :slight_smile: