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 stephen_george98 · Apr 17, 2016 at 03:36 PM · colliderobjecttutorialinvokepooling

How to Code Obstacle to Go Back Into My Object Pooler When They Collide With a Boundary?

Hello all,

please do not feel intimidated by my long question, I am sure I just worded it weird lol. I followed Mike Geig's excellent tutorial on using Object Pooling in Unity, and I get the concept pretty well. I just have a question on something that has been a major headache for almost a week to me. How do I make this obstacle go back into my Object Pooler when it Collides with the South Wall (which is tagged "SouthWall")? I have a picture and my scripts: alt text

(The Script Below is My Generic Pooler Script which I changed just a tad bit from the Object Pooling Tutorial)

  using UnityEngine;
     using System.Collections;
     using System.Collections.Generic;
     
     public class PoolerTestScript : MonoBehaviour
     {
         public static PoolerTestScript current;
         public GameObject spikeWall;
         public int wallPooledAmount = 20;
         public bool willGrow = true; // This will be false in the inspector and the game, but here I will keep it true so I dont mess anything up.
         
         private List<GameObject> wallPooledObjects;
         
         void Awake () 
         {
             current = this;
         }
     
         void Start ()
         {
             wallPooledObjects = new List<GameObject>();
             for(int i = 0; i < wallPooledAmount; i++)
             {
                 GameObject obj = (GameObject)Instantiate(spikeWall);
                 obj.SetActive(false);
                 wallPooledObjects.Add(obj);
             }
         }
         
         public GameObject GetPooledObject()
         {
             for(int i = 0; i< wallPooledObjects.Count; i++)
             {
                 if(!wallPooledObjects[i].activeInHierarchy)
                 {
                     return wallPooledObjects[i];
                 }
             }
             
              if (willGrow)
             {
                 GameObject obj = (GameObject)Instantiate(spikeWall);
                 wallPooledObjects.Add(obj);
                 return obj;
             }
             
             return null;
     
         }
         
     }

(The Script Below Is What Spawns the Obstacles, I got it from the Space Shooter Tutorial and changed it up some!)

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 
 public class Obstacle2 // Spike Wall Obstacle
 {
     public GameObject wall; // The second obstacle gameobject. This is attached in the inspector.
     public Vector3 spawnWPosValues; // Position where the second obstacle will be spawned at on the X,Y,Z plane. 
     public int wCount; // This is the count of the second obstacle in a given wave.
     public float wSpawnWait; // Time in seconds between next wave of obstacle 2.
     public float wStartGameWait; // Time in seconds between when the game starts and when the second obstacle start spawning.
     public float wWaveSpawnWait; // Time in seconds between waves when the next wave of obstacle 2 will spawn.
 }
 
 public class SpawnWalls : MonoBehaviour {
 
     public Obstacle2 obstacle2;
 
     void Start () {
     
         StartCoroutine (SpawnWall ());
         //InvokeRepeating ("Spawn", 1, 1);
 
         //Get reference to rigidbody, and set the speed
     }
     
 
     IEnumerator SpawnWall () {
 
         yield return new WaitForSeconds(obstacle2.wStartGameWait);
         while (true)
         {
             
             for (int i = 0; i < obstacle2.wCount; i++) {
                 
                 Vector3 spawnPosition_2 = new Vector3 (Random.Range(-obstacle2.spawnWPosValues.x, obstacle2.spawnWPosValues.x), 
                                                        obstacle2.spawnWPosValues.y, 
                                                        obstacle2.spawnWPosValues.z);
                 Quaternion spawnRotation_2 = Quaternion.Euler(0,270,0); // was 90, 0, 90 
                 Instantiate (obstacle2.wall, spawnPosition_2, spawnRotation_2);
                 yield return new WaitForSeconds(obstacle2.wSpawnWait);
             }
             yield return new WaitForSeconds (obstacle2.wWaveSpawnWait);
         }
     }
 }

(The Script Below Is What Moves the Obstacles)

     using UnityEngine;
     using System.Collections;
     
     public class WallObstacleMover : MonoBehaviour {
     
         private Rigidbody rb;      //Reference to Rigidbody Component
     
         public float speed;        //Speed, updated through script
         public float acceleration; //Every second, the speed will increase by this much
     
         //Executes once, when object is spawned / scene loaded
         void Start() {
             //Get reference to rigidbody, and set the speed
             rb = GetComponent<Rigidbody> ();
             rb.velocity = -transform.right * speed;
     
     }
         //Executes every frame
         void Update() {
             //Add acceleration to speed, make sure it's not above topSpeed)
             speed += Time.deltaTime * acceleration;
             //Set object velocity
             rb.velocity = -transform.right * speed;
     
         }
     }

unity-done-2-for-pooling.png (75.5 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to pickup objects with Kinect (using colliders) 0 Answers

Bouncing ball tutorial - Ball goes through the floor. 2 Answers

Get instance of the collider object 1 Answer

How to add collider to a Torus object 5 Answers

Concave collider error 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