Search Unity

Need a help with array in IEnumerator

Discussion in 'Scripting' started by ThierryH, Sep 18, 2014.

  1. ThierryH

    ThierryH

    Joined:
    Jul 1, 2014
    Posts:
    24
    Hello, I need a help, please.
    How I can announce an array of platform GO, and then use in IEnumerator?
    if i make GameObject [] platform, then I can`t use in IEnumerator.

    Code (CSharp):
    1. public class fallingPlatform : MonoBehaviour
    2. {
    3.     public GameObject platform;
    4.  
    5.     void OnControllerColliderHit(ControllerColliderHit hit)
    6.     {
    7.         if(hit.gameObject.tag == "Platform")
    8.         {
    9.             StartCoroutine(someIEnumerator(hit));
    10.         }
    11.     }
    12.  
    13.     IEnumerator someIEnumerator(ControllerColliderHit hit)
    14.     {
    15.         yield return new WaitForSeconds(2.0f);
    16.         print ("test");
    17.  
    18.         platform.rigidbody.useGravity = true;
    19.     }
    20.  
    21. }
     
  2. kral

    kral

    Joined:
    Aug 7, 2011
    Posts:
    25
    all you need to do is for loop.

    Code (CSharp):
    1. public class fallingPlatform : MonoBehaviour
    2. {
    3.     public GameObject[] platforms;
    4.     void OnControllerColliderHit(ControllerColliderHit hit)
    5.     {
    6.         if(hit.gameObject.tag == "Platform")
    7.         {
    8.             StartCoroutine(someIEnumerator(hit));
    9.         }
    10.     }
    11.     IEnumerator someIEnumerator(ControllerColliderHit hit)
    12.     {
    13.         yield return new WaitForSeconds(2.0f);
    14.         print ("test");
    15.         for(int i = 0; i < platforms.Length; i++){
    16.               platforms[i].rigidbody.useGravity = true;
    17.         }
    18.     }
    19. }
     
  3. ThierryH

    ThierryH

    Joined:
    Jul 1, 2014
    Posts:
    24
    thank`s man
     
  4. ThierryH

    ThierryH

    Joined:
    Jul 1, 2014
    Posts:
    24
    Can you help me a little more.
    Code (CSharp):
    1. void Start()
    2.     {
    3.         SetupCenterOfMass();
    4.         LaterUpdate();
    5.         // BoostGenerator();
    6.         makeBoost();
    7.         currentSpeed = forwardSpeed;
    8.     }
    9.  
    10.     void Update()
    11.     {............}
    12.        
    13.      IEnumerator LaterUpdate()
    14.     {
    15.         while(true)
    16.         {
    17.             StartCoroutine(HoverC());
    18.             yield return new WaitForSeconds(0.015f);
    19.          }
    20.     }
    21.  
    22.     IEnumerator HoverC()
    23.     {
    24.         HoverCar();
    25.         yield return new WaitForSeconds(0.015f);
    26.     }
    27.  
    28.     void makeBoost()
    29.     {
    30.         StartCoroutine(BoostGenerator());
    31.     }
    32.  
    33.     IEnumerator BoostGenerator()
    34.     {
    35.         while(true)
    36.         {
    37.             if(maxBoost < StartMaxBoost)
    38.             {
    39.                 maxBoost = maxBoost + boostRegenAmount;
    40.             }
    41.             else if(maxBoost > StartMaxBoost)
    42.             {
    43.                 maxBoost = StartMaxBoost;
    44.             }
    45.            
    46.             yield return new WaitForSeconds(10);
    47.         }
    48.     }
    49.  
    50.     void HoverCar()
    51.     {..............here the code to hovering the car}
    The problem is - that the void HoverCar() doesn`t work, it must be called from IEnumerator HoverC()