Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Prefab Instantiation call method after start

Discussion in 'Scripting' started by vladstorm_, Jan 9, 2014.

  1. vladstorm_


    Nov 26, 2013
    I have a problem.

    I have a class Label which is drawing the text. Label is depending of some resources (image characters and stuff).
    I also have a main object in my scene called Resources. When my game is loading. i'm loading resources.
    Code (csharp):
    1. public class Resources : MonoBehaviour {
    2.     public Dictionary<char, Sprite> chars; //chars for font
    4.     void Awake(){
    5.         loadFont("font33chars", chars); //for font 33
    6.     }
    7. }
    Then, i'm using these loaded characters to draw the text.
    Code (csharp):
    1. public class Label : MonoBehaviour {
    3.     void Start(){
    4.         chars = singleton.i.resources.chars;
    5.         drawText("ba bla "); //depending on chars
    6.     }
    Lets say I have many labels in my game and this works totally fine.
    Later I wanted to create this Label in runtime using prefab (which is in the resources folder).
    it's also pretty easy
    Code (csharp):
    1.         var pfLabel = (GameObject)Resources.Load("Label", typeof(GameObject));
    2.         var label = (GameObject)Instantiate(pfLabel, transform.position, Quaternion.identity);
    3.         label.GetComponent<whatever>().drawText("bla bla runtime")
    and here there's a problem. because when i'm calling method drawText (which is depending on method start) - i'm realizing that start didnt run yet. so, my characters are not loaded and i cant draw a text.

    i read this article
    but in my case i can't do it like this.
    I can't use Awake method in Label to load resources.
    because i'm using Label on the scene and as a prefab in the same time (maybe its wrong).
    and when it's loading on the scene then when Label.Awake is called - my resources are not ready.

    this is really confusing. i hope you understood. Tell me what to do in this situation.
  2. deadfire52


    Jun 15, 2011
    I think this solves your problem:

    From here:
  3. vladstorm_


    Nov 26, 2013
    yea i know... but i can't use myLabel.Awake. i just wrote. because when i'm using the object in the scene and unity is calling myLabel.Awake myResources(my class with resources) are not loaded yet.
    Im using myRecourses.Awake - to load resources and then myLabel.Start to get those resources..