Call a coroutine from prefab? Do I need a singleton?

Updated on April 12, 2019 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
6 on April 9, 2019

I’m working on an app in Unity. The problem is how to refresh the screen after you delete something from the database( PHP/MYSQL). Normally, I would destroy the objects then call the function that loads the data and displays the data(Coroutines) only after it destroys the data, it doesn’t start the coroutine as instructed.The script is on a prefab and I thought that if I simply copied the code from the other script, it would work. Alas, it did not.

 

When deleting, the script is on a prefab and it will not let me put the displayparent transform in the editor.

 

  public IEnumerator DeleteOilChange()
    {
        string ID = PlayerPrefs.GetString ("VehicleID");
        WWWForm OilDeleteForm = new WWWForm ();
        OilDeleteForm.AddField ("VehicleID", ID);
        OilDeleteForm.AddField ("ServiceDate", OilServiceDate);
        WWW OilDelete = new WWW ("http://localhost/CMVM/DeleteOilChange.php", OilDeleteForm);
        yield return OilDelete;
        Debug.Log (OilDelete.text);
        DestroyObjects ("OilChange");
    }
    public IEnumerator GetAllOilChanges()
    {
        string ID = PlayerPrefs.GetString ("VehicleID");
        WWWForm OilDataForm = new WWWForm ();
        OilDataForm.AddField ("VehicleID", ID);
        WWW OilData = new WWW ("http://localhost/CMVM/ViewOilChanges.php", OilDataForm);
        yield return OilData;
        Debug.Log (OilData.text);
        string OD = OilData.text;
        JSONArray VehicleArray = JSON.Parse (OD).AsArray;
        ShowAllOilChanges (OD);
    }
    public void DestroyObjects(string tag)
    {
        GameObject[] Obj = GameObject.FindGameObjectsWithTag(tag);
        if(Obj==null)
            return;
        else
        {
            Debug.Log(Obj.Length);
            for(int i=0; i< Obj.Length; i++)
            {
                Destroy(Obj[i]);
            }
            Debug.Log(tag + "Objects Destroyed!");
        }
        StartCoroutine("GetAllOilChanges");
    }
    public void ShowAllOilChanges(string OD)
    {
        Debug.Log("Show All Vehicles!");
        var vehicles = JSON.Parse(OD);
        foreach(JSONNode v in vehicles)
        {
            GameObject vObj = Instantiate(OilChangePrefab);
            vObj.GetComponent<OilChangeScript> ().DisplayOilChanges (
                "ID: " +    v["VehicleID"].Value,
                "ServiceDate: "    + v["ServiceDate"].Value,
                "Location: "    + v["ServiceLocation"].Value,
                "Mileage: "    + v["Mileage"].Value,
                "Labor: " +    v["Labor"].Value,
                "Oil Brand: "    + v["OilBrand"].Value,
                "Oil Price: "    + v["OilPrice"].Value,
                "Filter Brand: "    + v["FilterBrand"].Value,
                "Filter Price: " +     v["FilterPrice"].Value,
                "Purchase Place: "    + v["PurchasePlace"].Value
            );
            //vObj.transform.SetParent(OilDisplayParent); 
        }
    } 

 

Can anyone give me a hand with this? Do I need a singleton or is there another way?

 

  • Liked by
Reply
5 on April 9, 2019

Yes you can do coroutines on prefabs, no you don’t need a singleton, but if there is only ever going to be the 1 instance of this object in the scene, it probably should be.

I’m not sure why the coroutine isn’t running on the prefab. Is the prefab already in the scene on start, or is it spawned in at runtime?

And either way, how is it being called? A button? Could it maybe just be missing a reference?

on April 10, 2019

It’s being called first by a button, then by a click.

It’s a split screen view with the instantiated prefab on the left and a form on the right. When you click on the prefab, it will erase that entry from the database. When you enter info into the database, it needs to destroy the objects and instantiate with the new data. It works just fine when you enter data, it’s the deleting and redrawing that is the problem. I’m leaning towards a static function.

 

Wise
on April 10, 2019

Just to be clear, what object are you destroying? The info from the DB, or the prefab object?

on April 11, 2019

The prefab object

Wise
on April 11, 2019

The same object you’re trying to run the coroutine on?

I’m pretty sure if an object is destroyed it stops all the coroutines running on it.

 

Edit: Just checked, yes destroying an object stops all coroutines on it.

on April 12, 2019

Well, I tried the static function, but it wanted an instance for the non-static members. I think I’ll go for a singleton. Just have to gain access to the canvas the script is on. Will update…

 

Show more replies
  • Liked by
Reply
Cancel