check if gameObject is still inside trigger

This is a very basic and stupid question but I have this problem where I have a series of Spawn points where the players can spawn, I dont want two players to spawn on the same SpawnPoint so I made a little script with a Bool called IsOcupaid so I know if the Spawn point is occupied or not using a trigger, the problem here is if a player dies inside the trigger collider since the player didnt exit the trigger the Spawn point is still occupied so other players cant spawn there…

using UnityEngine;
using System.Collections;

public class SpawnPoint : MonoBehaviour {
    public bool IsOcupaid = false;
    // Use this for initialization
    void OnTriggerExit(Collider Other){
        TankController Temp = Other.GetComponent<TankController>();
        if (Temp != null){
            IsOcupaid = false;
        }
    }
    void OnTriggerStay(Collider Other){
        TankController Temp = Other.GetComponent<TankController>();
        if (Temp != null){
            IsOcupaid = true;
        }
   
    }
}

Any ideas how can I fix this problem?

by “dies” i assume you are using the Destroy function? one way is to add an “OnDestroy()” function to the player and have them move “up” 10000 (well outside the game area and out of view) before being destroyed, it’ll all happen within 1 frame so it shouldn’t be visible. The move will make the triggers react before the object ceases to exist.

might not work if you have a 3d environment though…

I dont destroy the players just disable them, but anyways I tested it using my “dead” function and moving the player just before disabling the player character, it doesnt work, it seem triggers are not fast enough and also since the camera follows the player this sudden movement is really noticeable.

Ok nevermind Im fucking stupid I didnt knew I could just use an “else” inside the OnTriggerStay and it would work, I thought you would have to use OnTriggerExit to check if the object its not there, but it seems the OnTriggerExit its not needed at all.

1 Like

I’m glad you’ve found a solution! In addition, I would make the IsOccupied bool a property, rather than a field, so it can’t be edited by outside code:

    bool _isOccupied = false;
    public bool IsOccupied {
        get {
            return _isOccupied;
        }
        private set {
            _isOccupied = value;
        }
    }
1 Like

You could also stick an update in there to check player status as well!

I just made IsOcupaid hold the current player in the spawn, so the update can check if it becomes dead, and free the spawn.

public TankController IsOcupaid = null;
   
void OnTriggerEnterCollider Other){
    IsOcupaid = Other.GetComponent<TankController>();
}
void OnTriggerExitCollider Other){
    if(IsOcupaid  == Other.GetComponent<TankController>()){
        IsOcupaid = null;
    }
}
void Update(){
    if(IsOcupaid != null && IsOcupaid.isDead){
        IsOcupaid = null
    }
}

Also, GetComponent is a heavy function, so it’s best not to put it in a function like OnTriggerStay that will be called very frequently.

Also also, why did you spell it “IsOcupaid”? :slight_smile: