Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Passing initial values to Awake()

Discussion in 'Scripting' started by cncguy, Jan 21, 2010.

  1. cncguy

    cncguy

    Joined:
    Oct 26, 2009
    Posts:
    53
    Hi. I was wondering if I could add a script component to a gameobject with parameters to pass to Awake? I realise I can create a component and call methods on it to initialise values and then add it to a gameobject, but it would be nice to create it with initialisation values like you would normally instantiate a class object.

    I guess another way to ask is: The suggestion is not to use the class constructor/destuctor. Is it possible (could it be possible in the future) to use the Awake() function with arguments?
     
  2. CoherentInk

    CoherentInk

    Joined:
    Jul 16, 2006
    Posts:
    216
    You can always specify default values when you declare your variables, i.e.:
    Code (csharp):
    1. var tester : float = 0.1;
    That way by the time Awake() is called the value will be set. Or are you looking for control over the values for each instance?
     
  3. cncguy

    cncguy

    Joined:
    Oct 26, 2009
    Posts:
    53
    I'm really looking for control each instance. I understand how to set initial values for variables. I want to be able to decide on the fly when the component is created what the initial values should be.
     
  4. zibba

    zibba

    Joined:
    Mar 13, 2009
    Posts:
    165
    What's going to make that decision? For example, you could do a few things:

    1. Have a public variable that can be edited in the inspector
    2. Make a manager which sets values (public variables or properties) on the object
    3. Awake is there to initialise the object before the game starts, so grab necessary values from somewhere
     
  5. cncguy

    cncguy

    Joined:
    Oct 26, 2009
    Posts:
    53
    Thanks zibba but the gameobjects that these components are on do not exist at game start. I am creating new gameobjects on the fly and adding components to them. The variables I want to initialise get initialised when the object and the script component are Instantiated. The initialisation values depend on which parent gameobject created them and which particular options on that script I want active, therefore It would be nice to pass these values to Awake() as parameters when the component is instantiated.

    I know I can create get/set/doSomething methods on the script component, call them after instantiating and then add the script to the gameobject. The point is its not very tidy.
     
  6. zibba

    zibba

    Joined:
    Mar 13, 2009
    Posts:
    165
    Yeah it's a little cumbersome... what would be really nice is if it was properly object-oriented and had dynamic binding then you could simply Instantiate and call the method directly. SendMessage sort of does that.
     
unityunity