my destroy wall code keeps destroying entire object for good
this script does kill gameobject after contact but it kills even the future ones
I want it to kill the one that touches wall only not all I want it to keep instantiating them but it stops because this script kills it off entirely
using UnityEngine; using System.Collections;
public class wall5killfish : MonoBehaviour { //public Rigidbody2D projectile; //private Rigidbody2D projectileInstance = null;
void Start()
{
}
//void OnCollisionEnter2D(Collision2D other)
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "wall5")
{
//Destroy(projectileInstance.gameObject);
Destroy (gameObject);
//if the bird collides with something set it to dead...
//isDead = true;
//GetComponent<AudioSource> ().Play ();
}
}
}
Answer by dCalle · Jan 29, 2017 at 10:57 PM
the script kills itself....
because you forgot to put other. in front of gameObject
Destroy(other.gameObject);
not working I put that on the gameobjects correct? either way it doesn't work
I dont want to destroy the wall I want to destroy the gameobects after they touch the wall
I think your "other" kills the wall
heres 1 of the scripts I have on them
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletFireScript : $$anonymous$$onoBehaviour {
public float fireTime = .05f;
public GameObject bullet;
public int pooledAmount = 20;
List< GameObject> bullets;
void Start () {
Debug.logger.logEnabled = false;
bullets = new List <GameObject> ();
for (int i = 0; i < pooledAmount; i++) {
GameObject obj = (GameObject)Instantiate (bullet);
obj.SetActive (false);
bullets.Add (obj);
}
InvokeRepeating ("Fire", fireTime, fireTime);
}
void Fire()
{
for (int i = 0; i < bullets.Count; i++)
{
if (!bullets [i].activeInHierarchy)
{
bullets [i].transform.position = transform.position;
bullets [i].transform.rotation = transform.rotation;
bullets [i].SetActive (true);
break;
}
}
}
}
^^ it's because you've only created some bullets and the script iterates through them and reuses the ones that are DISABLED.
replace`Destroy(gameObject);`
with gameObject.SetActive(false);
dcalle I think you right
I removed the bullet with the pool script and it works with the other bullet prefabs that DOTN have the pool script why is this happening I thought pooling was good =---- it seems like pooling is a bunch of trouble so wen I changed the prefabs to the regular bullets with no pool code it works perfect
why is this happening? is pooling bad?
nope champ. pooling is good. because it prevents your CPU from doing more than he needs to do.
Try it again with the pool script and the gameObject.setActive(false);
how does your bullet fly? do you have a script here?
Answer by mike1233 · Jan 30, 2017 at 12:25 AM
public class ObjectPool : MonoBehaviour
{
public static ObjectPool current;
public GameObject[] prefabs;
public List<GameObject>[] pooledObjects;
public int[] amountToBuffer;
public int defaultBufferAmount = 10;
public bool canGrow = true;
GameObject containerObject;
void Awake ()
{
if (current == null)
current = this;
else
Destroy(gameObject);
containerObject = new GameObject("ObjectPool");
pooledObjects = new List<GameObject>[prefabs.Length];
int index = 0;
foreach ( GameObject objectPrefab in prefabs )
{
pooledObjects[index] = new List<GameObject>();
int bufferAmount;
if(index < amountToBuffer.Length)
bufferAmount = amountToBuffer[index];
else
bufferAmount = defaultBufferAmount;
for ( int i = 0; i < bufferAmount; i++)
{
GameObject obj = (GameObject)Instantiate(objectPrefab);
obj.name = objectPrefab.name;
PoolObject(obj);
}
index++;
}
}
public GameObject GetObject( GameObject objectType)
{
for(int i=0; i<prefabs.Length; i++)
{
GameObject prefab = prefabs[i];
if(prefab.name == objectType.name)
{
if(pooledObjects[i].Count > 0)
{
GameObject pooledObject = pooledObjects[i][0];
pooledObjects[i].RemoveAt(0);
pooledObject.transform.parent = null;
return pooledObject;
}
else if(canGrow)
{
Debug.Log("pool grew when requesting: " + objectType + ". consider expanding default size.");
GameObject obj = Instantiate(prefabs[i]) as GameObject;
obj.name = prefabs[i].name;
return obj;
}
break;
}
}
return null;
}
public void PoolObject ( GameObject obj )
{
for ( int i=0; i<prefabs.Length; i++)
{
if(prefabs[i].name == obj.name)
{
obj.SetActive(true);
obj.transform.parent = containerObject.transform;
pooledObjects[i].Add(obj);
return;
}
}
}
}
well you don't need that script on the bullet haha. Don't you have a script that makes the bullet fly around?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletScript : $$anonymous$$onoBehaviour {
public float speed = 1;
// Update is called once per frame
void Update ()
{
//transform.Translate (0, speed * Time.deltaTime, 0);
transform.Translate (-speed * Time.deltaTime, 0, 0);
}
}
so its true that pool script was the problem correct?
Your answer
Follow this Question
Related Questions
Object Pooling Spawn Script 1 Answer
C# OnCollisionEnter not Triggering If Statement 1 Answer
I need help with my spawning system(SOLVED) 1 Answer
Checkpoint and Respawn 0 Answers
Buncha questions about scripts. 0 Answers