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.