Search Unity

Resetting OnTriggerStay

Discussion in 'Scripting' started by Holocene, Oct 17, 2016.

  1. Holocene

    Holocene

    Joined:
    Feb 21, 2009
    Posts:
    347
    This script executes perfectly, but does not work after I re-activate the object.
    I thought I needed to reset Time.deltaTime, but apparently that isn't the answer.
    How do I get this code to work more than once?
    Any help would be appreciated.




    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class OnTriggerStayTimerFlower_Bee_Pollinate : MonoBehaviour{
    4. public GameObject projectilePrefab1;
    5. public GameObject projectilePrefab3;
    6. float t = 0;
    7. private bool canInstantiateProjectile = true;
    8. public GameObject ActivateThis;
    9. public GameObject ActivateThis2;
    10.  
    11.      
    12.  
    13. void ResetTimer()
    14. {
    15. t = 0;
    16.  
    17. }
    18.  
    19.  
    20. void OnTriggerStay(Collider other)
    21. {
    22. if (other.gameObject.tag == "Bee")
    23. t += Time.deltaTime;
    24. if (t > 10)
    25. {
    26. if (canInstantiateProjectile)
    27. {
    28.                
    29. canInstantiateProjectile = true;
    30.                
    31. t = 0;
    32. GetComponentInParent<Animator>().Play("AnimationTest", 0);
    33.                
    34. ActivateThis.SetActive(false);
    35. ActivateThis2.SetActive(true);
    36.                
    37.  
    38. Instantiate(projectilePrefab1, transform.position, transform.rotation);
    39. Instantiate(projectilePrefab3, transform.position, transform.rotation);
    40.  
    41. canInstantiateProjectile = false;
    42.  
    43.                
    44. ResetTimer();
    45.             }}}}  
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    The reason why you can't ever fire a second projectile is this line right here:
    Code (CSharp):
    1. canInstantiateProjectile = false;
    Once you set that to false, it can never be turned back true. Because the line to turn it back true is INSIDE the check if its true. So it always checks false and never gets back into that line. You don't need a bool to check if you can fire, your timer is already doing that. Every 10 seconds you can fire, if not it skips the fire section. Just rip out everything do to with canInstantiateProjectile and it should work.

    The rest of the code seems like it should work as you wanted. Though a few notes:
    • if (t>10) is 10 seconds.. That is a long time in game terms. Are you sure you wan to wait 10 seconds between project fires?
    • What is ActivateThis and ActivateThis2? They are gameObjects but what are they for and what GameObjects are they.? Why are you turning one on, and the other off?
    • Does anything turn ActivatThis back on, and ActivateThis2 back off? Does it need to, because your code here isn't doing it.
     
  3. Holocene

    Holocene

    Joined:
    Feb 21, 2009
    Posts:
    347
    Takatok,
    This makes perfect sense and it's now working well.
    Thanks for taking the time to help!