Instantiated Object instantiate at 0, 0, 0

So im currently trying to make a individual asteroid “areas” spawn, but every time they instantiate they go to 0,0,0. Heres my code.

using UnityEngine;
using System.Collections;

public class AsteroidGenerator : MonoBehaviour {

	public GameObject asteroid;
	public GameObject asteroidSpawn;
	public Vector3 spawn;
	public int maxAmount = 1000;
	public int minDist = -500;
	public int maxDist = 500;

	// Use this for initialization
	void Start () {
		placeAsteroids();
		spawn = asteroidSpawn.transform.position;
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	void placeAsteroids(){
		for (int i = 0; i < maxAmount; i++) {
			Instantiate(asteroid, GeneratedPoition(), Quaternion.identity);
				}
	}
	Vector3 GeneratedPoition(){
		float x, y, z;
		x = spawn.x + Random.Range (minDist, maxDist);
		y = spawn.y + Random.Range (minDist, maxDist);
		z = spawn.z + Random.Range (minDist, maxDist);
		return new Vector3 (x, y, z);

	}
}

Your script looks right to me, however what I can’t see is what your variables are set as.

 public int minDist = -500;
 public int maxDist = 500;

These variables are public so when this script is on a GameObject you can change those variables in the inspector. They might be set to 0, overriding what you set in the script.

This sometimes happens to me as well when I write a public variable (int in this example) and forget to set a default value I want, compile it in unity and then remember to set a default value, but by then it’s too late. All objects currently with this script attached would initialise that value to 0.

I suggest you either, double check your object in the inspector, or remove public making those variables private and see if that helps.

Hey,
Your script seems to have one little bug.

Looking carefully.

 void Start () {
         placeAsteroids();
         spawn = asteroidSpawn.transform.position;
     }

You are placing Asteroids before giving spawn some value.

Correction

 void Start () {
         
         spawn = asteroidSpawn.transform.position;
         placeAsteroids();
     }

Also,all your variables are public.
Try making some like spawn, which are used only by your script private, so that nothing else can change the value and interfere with script.