Prefab spawning without running script (uses VR controllers)

Similar to most people on here I’m fairly new to the unity scene and I’m attempting to create a specific mechanism in my game: I want to create a bush where I can spawn fruit items from; the fruit on the bush take a specific amount of time to grow and can’t be interacted with until they’re fully grown. Once grown as soon as you grab the fruit, it spawns another in the position of the original to repeats the cycle.

However, how the code works is it spawns a first fruit, grows and interacts once grown but once I grab the first fruit, the plant stops growing and just shows the fully grown object(however after every time you grab that fruit it spawns another fruit as intended) ,this object doesn’t react with gravity unlike the original which applies gravity after interaction.

The object is a prefab and contains the following script:

using System.Collections;
using System.Collections.Generic;

using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;

public class GrowthScript : MonoBehaviour
{
Collider ObjectCol;
[SerializeField]
private GameObject Item;

public int GrowTime = 6;
public float MinSize = 0.1f;
public float MaxSize = 1f;
public float Timer = 0f;
private XRGrabInteractable grabInteractable = null;



public bool IsMaxSize = false;
public bool CanRegrow = false;

// Start is called before the first frame update

void Start()
{

    Debug.Log("Growing");
    IsMaxSize = false;
    ObjectCol = GetComponent<Collider>();
    // if the plant isnt full size, it starts a routine to grow
    if (IsMaxSize == false)
    {
        ObjectCol.enabled = !ObjectCol.enabled;
        StartCoroutine(Grow());

    }

}
private void Awake()
{
    grabInteractable = GetComponent<XRGrabInteractable>();
    grabInteractable.onSelectEntered.AddListener(Obtain);
    
}

private IEnumerator Grow()
{
    Vector3 startScale = new Vector3(MinSize, MinSize, MinSize);
    Vector3 maxScale = new Vector3(MaxSize, MaxSize, MaxSize);

    do
    {
        transform.localScale = Vector3.Lerp(startScale, maxScale, Timer / GrowTime);
        Timer += Time.deltaTime;
        yield return null;
    }
    while (Timer < GrowTime);
    IsMaxSize = true;
    CanRegrow = true;
    Debug.Log("Grown");
    ObjectCol.enabled = !ObjectCol.enabled;
}
private void Obtain(XRBaseInteractor interactor)
{
    if (CanRegrow == true)
    {
        
        GameObject instance = Instantiate(Item, transform.position, transform.rotation) as GameObject;
        CanRegrow = false;

    }
}

}

It would be Deeply appreciated if I could receive help on why the prefab doesn’t run the code on respawn or a way to solve the problem.

Much appreciated (Good Luck)

I start with indicating that the following lines are almost useless … you have just started the scene, so you expect the values to be the ones you set 2 lines above…

if (IsMaxSize == false)
{
     ObjectCol.enabled = !ObjectCol.enabled;
     StartCoroutine(Grow());
}

simply remove the if and start directly the coroutine…


Now, your problem comes from the fact that once you have harvested the fruit, you don’t reset the “isFull” variable to false and you don’t restart the growing method…

to resolve the issue, just use this code:

private void Obtain(XRBaseInteractor interactor) {
    if (CanRegrow == true) {
        GameObject instance = Instantiate(Item, transform.position, transform.rotation) as GameObject;
        CanRegrow = false;
        IsMaxSize = false;
        ObjectCol.enabled = !ObjectCol.enabled; // I think you want to have ObjectCol.enabled = false;
        StartCoroutine(Grow()); // Restarting the growing method
    }
}