- Home /
gameObject.setActive(false) is disabling the wrong objects
I have a script attached to my bullet prefab, which is responsible for basic movement and resetting the bullet back to the object pool after a set amount of time, I use Invoke for this. However when the character starts shooting at first it will work just fine, but after a while bullets start disappearing while there are still other bullets in front of them (alive longer) that keep going, so my first thought is that the bullet that is being reset too early is being reset by another bullet that shouldve reset itself..
Here is the script attached to the bullets:
public class BasicMovement : MonoBehaviour
{
[SerializeField] public GameObject spellSpawn;
private Rigidbody objectRigidbody;
public float bulletRange = 2f;
public float speed;
Vector3 _localForward;
void OnEnable()
{
spellSpawn = GameObject.FindGameObjectWithTag("SpellSpawn");
objectRigidbody = transform.GetComponent<Rigidbody>();
objectRigidbody.transform.position = spellSpawn.transform.position;
objectRigidbody.velocity = transform.forward * speed;
Invoke (nameof(DestroyBullet), bulletRange);
}
void DestroyBullet()
{
gameObject.SetActive(false);
}
}
My object pooler script, here:
public static ObjectPooler SharedInstance;
void Awake()
{
SharedInstance = this;
}
void Start()
{
pooledObjects = new List<GameObject>();
for (int i = 0; i < amountToPool; i++)
{
GameObject obj = (GameObject)Instantiate(objectToPool);
obj.SetActive(false);
pooledObjects.Add(obj);
}
}
public GameObject GetPooledObject()
{
//1
for (int i = 0; i < pooledObjects.Count; i++) {
//2
if (!pooledObjects[i].activeInHierarchy) {
return pooledObjects[i];
}
}
//3
return null;
}
Thanks for the help!
Answer by zevonbiebelbrott · Feb 21, 2021 at 04:36 PM
I GOT IT!!! THE staff that fires the spells pivots forward every shot as an animation, now the spawn of the bullets/spells was just infront of the staff, so the staff was actually hitting some of the bullets as they spawned making them collide immediately and despawn! All I had to do was move the spellspawn forward a little bit so the bullets wont get hit by the players staff when they are spawned in!
What a relief! I knew everything was alright with the pooler and how they are disabled, I have added a failsafe to the Invoke SetActive method, which is:
private void OnDisable(){
CancelInvoke ();
}
This way, if the bullet gets disabled before it had time to call the invoke, it wont try to call it as we cancel the Invocation on disable :D, this probably does nothing, because I suspect OnDisable all invocations are cancelled anyway, but its a nice failsafe either way..
Nice. I'd suggest you take the collider off the staff or put it in a layer that doesn't collide with the projectiles to make it impossible to happen. Sorry to send you down the wrong path!
Answer by MUG806 · Feb 20, 2021 at 10:25 PM
Could it be that you are spawning a bullet, triggering the invoke, but then despawning the bullet due to some other game logic (like the bullet hitting something maybe?), then the same object gets activated for a new spawned bullet, but the original invoke is still waiting, and fires off, deactivating the object? No idea if that is even possible, but just a random hunch.
It doesnt seem like it, the glitch is really ODD, when I hold down the mouse button to fire, sometimes they just travel for half a second and every single one does that, then if i let go of the mouse button, wait for a second and hold it down again, the bullets will fly all the way for 5 seconds like they should, but then after a while you can see that they start to glitch again and disappear before they should, I tried ai$$anonymous$$g only in to the sky so they cant hit anything and still it happens. BUT if they do hit a wall or the floor they also get disabled(setActive(false)) on Collision. The weirdest is that this code is on the bullet itself, so it cant just select random "bullet" from the hierarchy and delete that one right? It can only delete itself you would think... since there is no reference to any other bullet in the script.
Hmm yeah it's certainly odd. I'm sure it must be related to the object pooling somehow as I can't think of any other way for the wrong object to be effected.
What if the objects are somehow getting fired twice (the pooling returning an object that is already on the scene somehow).
What does the code look like for shooting and destroying on collision etc?