Game object spawning, spawn conditions and spawning location problems.

Hi,

I have this script that is meant to randomly instantiate an apple somewhere within the play area if another apple does not exist within the play area. I am having two problems with this code/unity.

Problem 1: The if statement to check if an apple already exists does not work. An apple does spawn if I remove the if statement though. So I know it is the if statement and something is likely wrong with the wording.
Here is the code:

public class AppleSpawn : MonoBehaviour 
{
	public GameObject Apple;
	float PlaceX;
	float PlaceY;
	float PlaceZ;
	public float spawnWait;
	public float SpawnMostWait;
	public float spawnLeastWait;
	public int startWait;
	public bool stop;


	void Start () 
	{
		if (GameObject.FindGameObjectsWithTag ("FoodBin") == null) {
			StartCoroutine (BringMore ());
		}
	}
		
	void Update () 
	{
		PlaceX = Random.Range (-13, 22);
		PlaceY = Random.Range (0, 1);
		PlaceZ = Random.Range (-24, -24);

		spawnWait = Random.Range (spawnLeastWait, SpawnMostWait);
	}

	IEnumerator BringMore ()
	{
		yield return new WaitForSeconds (startWait);
		while (!stop) 
		{
			Vector3 spawnPosition = new Vector3 (PlaceX, PlaceY, PlaceZ);
			Instantiate (Apple, spawnPosition + transform.TransformPoint (0,0,0), gameObject.transform.rotation);
			yield return new WaitForSeconds (spawnWait);
		}
	}
}

Problem 2: The apples are not appearing within the play area but are instead appearing at the center of the canvas. I have set the x,y,z values based on moving a game object about the scene so I am not sure why the coordinates that gives me are not translating properly. It is kind of amusing though. Here’s what it looks like for reference:

Thank you for any help you can give! If anything is unclear just say.

There are alot of stuff that you need to work on

  1. You are storing random positions randomly on Update hence too much of memory usage.

     void Update () 
     {
         PlaceX = Random.Range (-13, 22);
         PlaceY = Random.Range (0, 1);
         PlaceZ = Random.Range (-24, -24);
    
         spawnWait = Random.Range (spawnLeastWait, SpawnMostWait);
     }
    
  2. The stuff you need is as simple as

        private Vector3 applePos;
        private bool AppleExist = false;
        private float waitTime;
        public float waitForseconds;
        public GameObject apple;
        
        private void Start()
        {
            waitTime = Time.time + waitForseconds;
            StartCoroutine(AppleSpawn());
        }
        
        //Through Update
        private void Update()
        {      
               if(Time.time > waitTime)
                   {
                            waitTime = Time.time + waitForseconds;
                            if(!AppleExist)
                                  {
                                         applePos.x = Random.Range(0,25);
                                         applePos.y = Random.Range(0,25);
                                         applePos.z = Random.Range(0,25);
         
                                        GameObject go = GameObject.Instantiate(apple,applePos,Quaternion.Identity);    
                                         AppleExist = true;                         
                                  }
                   }
    
             if(AppleExist)
             {
                      //Do anything you want.(eg : delete Apple)
              }
        }