- Home /
activate and speed in z axis from a pooled inactive object!!
trying to add speed from a cloned GameObject in the z axis from a pool created at start up. any idea how i would go about that? 1. either add speed at activate in first script or 2. attach a script to prefab to acitvate at void Awake() add velocity or force onto the clone?
second question when objects are pooled at start they come out active? how would you have it inactive/disabled at start?
using UnityEngine;
using System.Collections;
public class activatPrfab : MonoBehaviour
{
public Pool pool;
public float time = 5;
private Vector3 ringPos;
GameObject[] instPoints;
// Use this for initialization
void Start ()
{
StartCoroutine(Countdown());
}
void activ()
{
GameObject[] instPoints = GameObject.FindGameObjectsWithTag("instant");
int count = Random.Range (0,3);
ringPos = new Vector3(instPoints[count].transform.position.x,instPoints[count].transform.position.y,instPoints[count].transform.position.z);
pool.activate(0, new Vector3(ringPos.x, ringPos.y, ringPos.z), Quaternion.Euler(90, 0, 0));
Debug.Log (count);
StartCoroutine(Countdown());
//for (int count = 0; count < instPoints.Length; count++)
//{
//}
}
IEnumerator Countdown()
{
for (float timer = time; timer >= 0; timer -= Time.deltaTime)
yield return 0;
activ();
Debug.Log("This message appears after"+time+"seconds!");
}
}
pooling script pools at run time script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Pool : MonoBehaviour {
public GameObject[] objects;
public int[] No;
public List<GameObject>[] pool;
// Use this for initialization
void Start ()
{
instantiate();
}
void instantiate()
{
GameObject temp;
pool = new List<GameObject>[objects.Length];
for(int count = 0; count < objects.Length; count++)
{
pool[count] = new List<GameObject>();
for(int num = 0; num < No[count]; num++)
{
temp = (GameObject)Instantiate(objects[count]);
temp.transform.parent = this.transform;
pool[count].Add(temp);
}
}
}
public GameObject activate(int id)
{
for (int count = 0; count < pool[id].Count; count++) {
if (!pool [id] [count].activeSelf) {
pool [id] [count].SetActive (true);
return pool [id] [count];
}
}
return null;
}
public GameObject activate(int id, Vector3 position, Quaternion rotation)
{
for (int count = 0; count < pool[id].Count; count++)
{
if (!pool [id] [count].activeSelf)
{
pool [id] [count].SetActive (true);
pool [id] [count].transform.position = position;
pool [id] [count].transform.rotation = rotation;
return pool [id] [count];
}
}
return null;
}
}
I didn't proofread your code, sorry.
Object pooling isn't terribly complicated. Whether they "come out active" is up to you, but for instance, my pooling script enables objects when they're requested and disables them when they return to the pool.
Any additional action you need to perform that isn't directly related to pooling should be separated from your pooling logic. This allows your pooling script to be "just" a pooling script.
In other words, you want something like:
GameObject go = Pool.Spawn(myPrefab);
go.rigidbody.AddForce(whatever);
The wording of your question is confusing, so I'm not sure what exactly you're asking. Please attempt to use proper English grammar and choose your words carefully.
Answer by samussa201 · Mar 09, 2015 at 12:17 PM
tank you so much for taking your time to help out, really appreciated. ended up adding this to my script and made them inactive at start. temp.SetActive(false). and worked great.
void instantiate()
{
GameObject temp;
pool = new List<GameObject>[objects.Length];
for(int count = 0; count < objects.Length; count++)
{
pool[count] = new List<GameObject>();
for(int num = 0; num < No[count]; num++)
{
temp = (GameObject)Instantiate(objects[count]);
temp.transform.parent = this.transform;
temp.SetActive(false);
pool[count].Add(temp);
}
}
and added this to the game object, just have to make sure the gameObjects facing the right direction.
void Update() {
transform.Translate(Vector3.back * Time.deltaTime);
}