Spawning prefab problem.

Hey all,

I’ve been working on this script for a while, I’ve got a lot of help from other peoples questions.

I have it currently working for the most part. I just cant seem to get the first spawn to be at random locations.

Here’s the code
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class FoodSpawner : MonoBehaviour {


	public float range;
	public GameObject food;
	
	public List<GameObject> spawnList;
	
	// Use this for initialization
	void Start () 
	{
		spawnList = new List<GameObject>();
		InvokeRepeating("FindFood", 1, 1);//(methodname, time, repeat time)
		
		//Populate for the first time
		for (int i = 0; i < 10; i++)
		{
			spawnList.Add(Instantiate(food) as GameObject); //spawnList.Add(new GameObject(food.ToString()));  //Time.timeSinceLevelLoad.ToString()));
		}
	}
	
	void FindFood()
	{
		Vector3 randomPos = Random.insideUnitSphere * range;
		randomPos.z = 0;
		for (int i = 0; i < spawnList.Count; i++)
		{
			if(!spawnList*)*
  •  	{*
    

_ spawnList = Instantiate(food, (transform.position + randomPos), Quaternion.identity) as GameObject; //GameObject.CreatePrimitive(PrimitiveType.Cube);_

* }*
* }*
* }*
}
I’m still somewhat new at this stuff. So go easy on me :stuck_out_tongue:
Thanks

Your first instantiate isn’t getting passed a spawn position; you’re just calling Instantiate(food) which will not set the newly instantiated objects position. Based on the code you posted, I think a solution would be to remove the loop under ‘Populate for the first time’ and instead pass 0 seconds in as the second argument to InvokeRepeating on line 13 so that FindFood is called immediately.

Why not try clamping the vector3 within a screentoworldpoint or worldpointtoscreen and instatiation should be simpler…
http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/2d-catch-game-pt2?playlist=17093 has a great spawner, adapt it to fit your list needs and you should be fine.