Search Unity

Question Plant growth script is not working

Discussion in '2D' started by bwool, May 31, 2023.

  1. bwool

    bwool

    Joined:
    Mar 30, 2023
    Posts:
    22
    Hello, I'm trying to make a plant-oriented game in 2-D, and the growth script I'm trying to build in't working.

    I have the script attached to an empty game object, and the sprites of the three growth stages are hidden child objects. Once I run the game, however, nothing is changing.

    Here is my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class pTreeSprout : MonoBehaviour
    6. {
    7.     private int currentProgression = 0;
    8.     public GameObject pSprout;
    9.  
    10.     private void Start()
    11.     {
    12.         InvokeRepeating("growth", 0, 3);
    13.     }
    14.  
    15.     void growth(GameObject pSprout)
    16.     {
    17.         if (currentProgression != 3)
    18.         {
    19.             gameObject.transform.GetChild(currentProgression).gameObject.SetActive(true);
    20.         }
    21.  
    22.         if (currentProgression > 0 && currentProgression < 3)
    23.         {
    24.             gameObject.transform.GetChild(currentProgression - 1).gameObject.SetActive(false);
    25.         }
    26.  
    27.         if (currentProgression < 3)
    28.         {
    29.             currentProgression++;
    30.         }
    31.     }
    32. }
    33.  
    Here is the error I'm getting:

    Screenshot 2023-05-31 at 1.38.21 AM.png

    Thank you for your help, and apologies if this is a dumb question!
     
  2. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    490
    1. The function you are calling requires a parameter.
    2. Your InvokeRepeating is searching for growth() without parameters (the console shows you this).
    3. No, you can't use a function with parameters to use it in InvokeRepeating.
    4. Value pSprout must be a class member (and you have it, so delete param from function) or you need to use a coroutine (if you want param).
     
    bwool likes this.
  3. bwool

    bwool

    Joined:
    Mar 30, 2023
    Posts:
    22
    That worked, thank you!