Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Plant growth script is not working

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

  1. bwool

    bwool

    Joined:
    Mar 30, 2023
    Posts:
    20
    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:
    437
    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. Bricied

    Bricied

    Joined:
    Apr 11, 2023
    Posts:
    5
    Hi @bwool,

    Hope so you're doing fine, upon reviewing the code, I noticed a potential issue with the growth method. It takes a GameObject parameter called pSprout, but it seems that this parameter is not being used within the method.

    To fix this, you can remove the GameObject pSprout parameter from the growth method since it's not necessary in this context.

    Additionally, make sure that you have properly assigned the pSprout game object to the script in the Unity Inspector. Check that the child objects representing the different growth stages are correctly attached to the empty game object as hidden child objects.

    Hope so this will work for you :)!

    Regards,
    Bryce June
     
    Last edited: Jun 4, 2023
  4. bwool

    bwool

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

     
  5. Bricied

    Bricied

    Joined:
    Apr 11, 2023
    Posts:
    5
    My Pleasure