Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mango14 · May 07, 2018 at 05:12 PM · 2dpickuppoolinginfinite runnercoin

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);
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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).

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mango14 · May 07, 2018 at 05:53 PM 0
Share

Any **simple**suggestions on getting around this? I have it deactivated to remove it from screen. Thanks!

avatar image Vicarian mango14 · May 07, 2018 at 06:02 PM 0
Share

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?

avatar image
0

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.

alt text


bird.png (178.6 kB)
gamecontrol.png (235.3 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mango14 · May 08, 2018 at 12:41 AM 0
Share

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.....

Video of Gameplay

avatar image Vicarian · May 08, 2018 at 12:59 AM 0
Share

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;
     }
 }
avatar image mango14 Vicarian · May 08, 2018 at 02:12 AM 0
Share

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).

another quick video

avatar image Vicarian mango14 · May 08, 2018 at 02:38 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

166 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges