- Home /
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.
Answer by bhavinbhai2707 · Jul 10, 2018 at 07:53 AM
There are alot of stuff that you need to work on
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); }
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) } }
Hi, thanks for the help. Unfortunately while it is spawning the initial apple, no more after that. It also isn't checking for an apple after that point (I added a debug.log to check both of these things).
I think it might be due to my Bring$$anonymous$$ore coroutine, when active it just spawns endless apples in the original location. Could you please tell me what you put into the Applespawn one?
Also the go in GameObject go comes from where? It's throwing up a yellow error in the inspector. It says the value is never used.
Sorry, I am still very much an amateur programmer. This project is mostly about self study.
@Lancerskytail Okay sorry for the mistakes and untested script(i typed it in phone so) and my bad explanations.
Here is the Updated,Tested and working Script
private Vector3 applePos; //tot store new position of Apple
private bool AppleExist = false; //to check if apple exist in scene or not
private float waitTime;
public float waitForSeconds; //how many seconds to wait
public GameObject apple; //Assign Apple model/Prefab from inspector here
private GameObject temp; //to Store the spawned apple
private void Start()
{
waitTime = Time.time + waitForSeconds; eg:-suppose Time.time is 7s right now and waitForSeconds is 2f then waitTime will be 9s. (check its usage in update)
}
private void Update()
{
if(Time.time > waitTime) //(eg :- 10s > 9s)
{
waitTime = Time.time + waitForSeconds;
if(!AppleExist)
{
//Storing new position of apple
applePos.x = Random.Range(0, 10);
applePos.y = Random.Range(0, 10);
applePos.z = Random.Range(0, 10);
temp = GameObject.Instantiate(apple, applePos, Quaternion.identity); //spawning the Apple and storing in temp to do further actions.
AppleExist = true; //stating that now apple does exist in scene
}
}
if(AppleExist)
{
//Apple exist is bool basically stating if apple exist in scene or not. If apple Exist in scene then you can do some task with it like relocate,delete etc.(like i did in example below)
if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.E))
{
GameObject.Destroy(temp); //Deleting apple from scene
AppleExist = false; //stating apple does not exist in scene anymore
}
}
}
Applespawn was a mistake, i forgot to remove it.
GameObject go is used to store the spawned apple,so that you can perform any other task you want (like deleting,relocating etc). Its not important to use though.
NOTE :- in order to spawn Another apple just make AppleExist = false;
The Script it working fine and should solve your issue.
I hope it helps
$$anonymous$$eep Learning :)
Hi, thanks for all the help. I found the issue was that I had a script on the apple prefab that added an apple to the apple count and destroyed it. This meant no new apple appeared.
So I added second if statement (the if (AppleExist) part) in the other script and told Applespawn to set AppleExist to false after the apple was destroyed from there. Now it works like a charm.
So thanks again for the help and quick replies!