Hi,
I want to make a game in which several (at the moment 2) players each have a tank and have to conquer the other side’s base. I have a lot of buildings in the game that potentially slow it down a lot. So, I wanted to disable all objects that are not close to either player.
The first thing i did was to pool all Monobehaviours in an array when starting up the Game Manager Script (I will move this to Awake() before compiling, but at the moment awake is not started as the game manager already exists in the scene)
m_Scripts = FindObjectsOfType<MonoBehaviour>() as MonoBehaviour[]; //Pool all Scripts on the Map
My tanks are stored in another array in this class and each tank has a component that contains the trigger (a sphere collider I called activationSphere). So within the gameManager on every update of the playcycle, I want to call a function DisableByDistance(MonoBehaviour script) that checks whether the elements of Scripts are outside of the activationSpheres of all tanks. To this end I wrote:
private void DisableByDistance(MonoBehaviour script) //Disable pooled Monobehaviour "script" if it is not close to any player
{
int tankCounter = 0; // Counts the number of tanks NOT close to the Monobehaviour
for (int i = 0; i < m_Tanks.Length; i++)
{
Collider activationSphere = m_Tanks[i].m_Instance.transform.FindChild("ActivationSphere").GetComponent<Collider>();
//pseudocode:
If not (activationSphere intersects script.collider) then tankCounter++;
}
//pseudocode:
if (tankCounter==m_Tanks.Length) //i.e. if no tanks are close to the monobehaviour
then disable script;
else
enable script;
}
So, I think that in principle this should work, but I have no clue how to code this. The colliders on the scripts are of different type (should I add specific colliders just for this task to have the same kinds of colliders?) and somehow I cannot access any of the methods OnTrigger… that should be accessible from the collider on the tank.
It would be awesome if someone could help me. I guess for an experienced user, this is a trivial problem,
Thanks a lot!!!
bustee
PS: Please feel free to propose totally different ways to achieve what I want. I am inexperienced and this was the first that came to my mind, so it is probably not very efficient.
PPS: I build this game on top of the Tanks! tutorial for unity beginner, so some of the notation might look familiar (if you did the tutorial).