- Home /
cannot respawn pickup objects infinite runner / flyer
Still getting my n00bish brain racked --- game is infinite flyer with fuel pickup. basically, my fuel objects spawn but once they are picked up, they are deactivated. they are never reactivated. This means eventually you will run out of fuel.
I watched a tutorial on object pooling but i'm missing something easy.
/ Object Pooling Script/ using UnityEngine; using System.Collections;
public class FuelPool : MonoBehaviour
{
public GameObject fuelPrefab; //The column game object.
public int fuelPoolSize = 5; //How many columns to keep on standby.
public float spawnRate = 3f; //How quickly columns spawn.
public float fuelMin = -1f; //Minimum y value of the column position.
public float fuelMax = 3.5f; //Maximum y value of the column position.
private GameObject[] fuels; //Collection of pooled columns.
private int currentFuelColumn = 0; //Index of the current column in the collection.
private Vector2 objectPoolPosition = new Vector2 (-15,-25); //A holding position for our unused columns offscreen.
private float spawnXPosition = 15.5f;
private float timeSinceLastSpawned;
void Start()
{
timeSinceLastSpawned = 0f;
//Initialize the columns collection.
fuels = new GameObject[fuelPoolSize];
//Loop through the collection...
for(int i = 0; i < fuelPoolSize; i++)
{
//...and create the individual columns.
fuels [i] = (GameObject)Instantiate(fuelPrefab, objectPoolPosition, Quaternion.identity);
}
}
//This spawns columns as long as the game is not over.
void Update()
{
timeSinceLastSpawned += Time.deltaTime;
if (GameControl.instance.gameOver == false && timeSinceLastSpawned >= spawnRate)
{
timeSinceLastSpawned = 0f;
//Set a random y position for the column
float spawnYPosition = Random.Range(fuelMin, fuelMax);
//...then set the current column to that position.
fuels [currentFuelColumn].transform.position = new Vector2(spawnXPosition, spawnYPosition);
//Increase the value of currentColumn. If the new size is too big, set it back to zero
currentFuelColumn ++;
if (currentFuelColumn >= fuelPoolSize)
{
currentFuelColumn = 0;
Debug.Log (currentFuelColumn);
}
}
}
}
/ Flyer Script for pickup /
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("FuelSource"))
{
other.gameObject.SetActive(false);
CurrentFuel = CurrentFuel + 25;
FuelSpawn();
}
}
void FuelSpawn()
{
Vector3 position = new Vector3(Random.Range(7, -15), 1, Random.Range(13, -5));
Instantiate(fuel, position, Quaternion.identity);
}
Answer by Vicarian · May 07, 2018 at 05:19 PM
If your FuelPooling script resides on the object that the player's ship collides with initially, the line other.gameObject.SetActive(false);
will prevent the Update method on the pooling script from firing, because that line disables the object the script is on. A disabled GameObject doesn't have its scripts fire (usually).
Any **simple**suggestions on getting around this? I have it deactivated to remove it from screen. Thanks!
You might need to clarify how you have your scene set up. Where do you have the FuelPooling script? Is it on something like a Game$$anonymous$$anager or GameControl object that has just a transform and contains some scripts? I notice the pooling object is instantiating fuel objects, and I suspect you modified the Flyer script to instantiate a fuel to try to force one to show up?
Answer by mango14 · May 07, 2018 at 11:34 PM
attached are 2 screenshots. On the game object bird ( the flyer), i have the bird script as well as attached the fuel prefab for pickup.
then, i have a gamecontrol object that contains other scripts. Included on this is a fuel pool script.
Answer by Vicarian · May 08, 2018 at 12:11 AM
Ok, I didn't notice this before. Two things:
First, I presume you're working with a 2D game, so anything on the Z axis is ignored, and will appear at the same depth regardless of position given orthographic projection.
Second, the math for the Random.Range call is wrong.
Vector3 position = new Vector3(Random.Range(7, -15), 1, Random.Range(13, -5));
The range is calculated using subtraction (max - min), so when this processes, you'll find that all the fuel objects will pop between -23 and -15 on the x axis, which is likely offscreen.
I'll take a look at what you sent above. The -15,-15 (from what i understand) is just a holding position for the objects internally. I got this from a unity tutorial.
If you look at the video below -- objects are initially spawned. When i pick them up, they are deactivated. If i do not pick them up, they will automatically respawn. Just can't figure out how to get the deactivated objects to respawn.....
For the initial run, you'll notice when you start, your prefabs get instantiated and then set active, but the renderers aren't yet enabled. Then, as the bird approaches a certain point on the stage, their sprite renderer is toggled on. As you intersect with the fuel, you disable it. This I think is where things are going wrong. The stage changes positions when your bird reaches a certain point later on. I think what should happen is that the fuels should have their sprite renderer toggled back on when the stage shifts, but they can't since they've been disabled. There's likely code somewhere that uses Send$$anonymous$$essage to do the state shift, but Send$$anonymous$$essage doesn't go to disabled GameObjects. So, change the OnTriggerEnter2D(Collider2D other) method to the following:
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("FuelSource") && other.gameObject.GetComponent<SpriteRenderer>())
{
other.gameObject.GetComponent<SpriteRenderer>().enabled = false;
CurrentFuel = CurrentFuel + 25;
}
}
i've added this and it looks like the objects are spawned, but the renderer is disabled.
i made another quick video with scenery removed (if that helps).
There's some file that sets the renderers on the fuel to active during your run in the first video (which is much more clear as to what's going on) that you haven't listed. That file will be key to getting things working. I'm thinking there's an event trigger or a Send$$anonymous$$essage statement that activates the renderers either when the bird gets in range or collides with or enters the trigger of something invisible. Spawning a new object with the bird on collision without destroying anything is a good way to run the mobile this is clearly intended to run on out of memory.
Your answer
Follow this Question
Related Questions
Rigidbody2d.velocity making character stop in between 1 Answer
camera follow with distance 1 Answer
2D UFO Tutorial, UFO turns invisble after 6th pickup? 1 Answer
2d platformer, coding coin collectable? 0 Answers
Sprite generation 1 Answer