Search Unity

OnTriggerEnter

Discussion in 'Scripting' started by Azux, Oct 13, 2019.

  1. Azux

    Azux

    Joined:
    Mar 3, 2015
    Posts:
    19
    Hi, i would like to do something after OnTriggerStay check everthing i ask for. So I have some object on my map i want to add only those which have contact with trigger and when i collect them all i need to do something. My problem is that i don't know how to start it AFTER i got them all.
    Code (CSharp):
    1.    
    2. public void OnTriggerEnter(Collider other){
    3.  
    4.         if(other.tag == "OBJECT")
    5.         {
    6.  
    7.             if(!powerSupplyTo.Contains(other.gameObject))
    8.                 powerSupplyTo.Add(other.gameObject);
    9.         }
    10. }
    11.  
    So here i collect all object i need but i cannot put "AddPowerTO(powerSupplyTO)" here because it will do it as many times as many object will collider with my Trigger. So i need first to get them all and then "AddPowerTO(powerSupplyTO)"
     
  2. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    well you could add public int MaxObjectsCount and int CurrentObjectsCount; for example and set the Max number you want through inspector.you can leave currentObjects private.
    and each time you pick one up, say CurrentObjectsCount++;
    and then check if currentObjects == MaxObjects
    if so ... do something.
    Or you could add public int MaxCount and then after each pickup
    check if ( powerSupplyTo.count == MaxCount)
    if so, do something.(of course only if the powerSupplyTo is list of pick-ups only.
     
  3. Azux

    Azux

    Joined:
    Mar 3, 2015
    Posts:
    19
    Ye i had same idea but in my case i cannot predict maxObjectCount.
     
  4. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    you can't set it in runtime also?
     
    Last edited: Oct 13, 2019