Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by samvilm · Dec 09, 2021 at 01:39 PM · triggercollision detectiontriggerspoolingpool

move pooled object on collision/trigger but keep pooling it

I have a number of pooled moons that cycle past the player. The player scores when he collides with one. At the moment the moon isn't affected by the collision but I want it to disappear on collision and re-join the pool. I don't want to destroy and respawn it because its bad for performance. Any ideas? It has to be triggered by ontriggerenter2d because that's how my scoring system is set up. Thanks!

Here is my pooling code (the object move code is separate):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class moonPool : MonoBehaviour
 {
     public int moonPoolSize = 5;
     public GameObject moonPrefab;
     public float spawnRate = 4f;
     public float moonMin = -1f;
     public float moonMax = 3.5f;
 
     
     private GameObject[] moons;
     private Vector2 objectPoolPosition = new Vector2 (-10f, -25f);
     private float timeSinceLastSpawned;
     private float spawnXPosition = 14f;
     private int currentMoon = 0;
 
     // Start is called before the first frame update
     void Start()
     {
         moons = new GameObject [moonPoolSize];
         for (int i = 0; i < moonPoolSize; i++)
         {
             moons [i] = (GameObject)Instantiate (moonPrefab, objectPoolPosition, Quaternion.identity);
         }
     }
 
     // Update is called once per frame
     void Update()
     {
         timeSinceLastSpawned += Time.deltaTime;
         if (GameControll.instance.gameOver == false && timeSinceLastSpawned >= spawnRate)
         {
             timeSinceLastSpawned = 0;
             float spawnYPostion = Random.Range (moonMin, moonMax);
             moons[currentMoon].transform.position = new Vector2 (spawnXPosition, spawnYPostion);
             currentMoon++;
             if (currentMoon >= moonPoolSize)
             {
                 currentMoon = 0;
             }
         }
     }
 }
 
Comment
Add comment · Show 3
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 CodesCove · Dec 09, 2021 at 05:26 PM 0
Share

Object pools usually have a method to fake the normal destroy by setting the game object to in-active state.

It's usually good idea to make the object pool in a generic way so you can use it with any gameobjects

Check here instructions: https://learn.unity.com/tutorial/introduction-to-object-pooling#

avatar image samvilm CodesCove · Dec 10, 2021 at 12:03 AM 0
Share

interesting. I got my code from a different unity learn tutorial. I wonder why they didn't do it properly.

avatar image samvilm samvilm · Dec 10, 2021 at 12:55 AM 0
Share

so i followed that tutorial but am now stuck at #7 of said tutorial.

How do i convert my original instantiate code to what they say? my current code is: { moons [i] = (GameObject)Instantiate (moonPrefab, objectPoolPosition, Quaternion.identity); }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by samvilm · Dec 11, 2021 at 10:07 PM

The solution that works in the end is: (in the PLAYER script)

 void OnTriggerEnter2D(Collider2D col)
  {
      if (!isdead)
      {
         col.gameObject.transform.position = new Vector2 (-10f, -25f);
      }
  }
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

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

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

Checking when two triggers intersect 1 Answer

How do I detect a collision between two objects using Bolt? 1 Answer

Enemies spawning on top of each other,Enemies spawn on top of each other 0 Answers

Can I know which exactly of an object's collider resulted in OnTriggerEnter()? 0 Answers

Finding contact point in OnTriggerEnter2D? 0 Answers


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