Search Unity

[UnityScript] How to pass arguments to a script inside instantiated prefab before it gets executed?

Discussion in 'Scripting' started by JorobusLab, Jul 13, 2014.

  1. JorobusLab

    JorobusLab

    Joined:
    Jul 3, 2014
    Posts:
    78
    Hi guys, I'm having some troubles with UnityScript and was writing about 200 lines explainig exactly what I'm trying to achieve, but I realized that is nonsense, will confuse you, so I'll go straight.
    I come from JavaScript(get mocked by Unity because of they call JavaScript to UnityScript in API) and Ruby and there's some mecanics of UnityScript that I can't understand, there's no many information about some things, for example...
    From one script A.js(attached to an empty GameObject) I'm trying to create another game objects from a Prefab(the prefab consist of an empty GameObject with a script B.js attached). The issue is that I must pass 5 arguments(which are not always the same, also they aren't random, they get calculated in A) to that Prefabs BEFORE they start to work.

    1) So first I've tried to make a constructor in B, accepting the 5 arguments and setting the variables from them to start to work. That never worked, trust me that I gave many arounds and Mono never let me build the scripts.

    2) I've read over there that I can't do the 1st approach, since Unity and Mono engine doesn't work that way. They suggest to approach by Instantiate() . So I did it, but....HOW DO I PASS THE 5 ARGUMENTS TO B? Instantiate() just receive 3 arguments: the object which must be instantiated, position and rotation. What I did is to instantiate the object and then I set manually each variable of B:

    // this code is inside a function
    var p : GameObject = Instantiate(prefab);
    var pScript : B = l.GetComponent(B);
    pScript.phaseLength = phaseLength;
    pScript.color = colorAUtilizar;
    pScript.steps = steps;
    pScript.x = x;
    pScript.y = y;
    pScript.character = letterToRepresent;

    ...but my concern is, how this really work? When I instantiate, does the script B get executed instantly? Or it ways until the function which instantiated the prefab ends? Because this is important. Can I set those variables before B get executed? Would you take some other approach?

    I have some other questions roaming my mind but I'll stop just with these by now. Thanks for your time and help.
     
  2. RockoDyne

    RockoDyne

    Joined:
    Apr 10, 2014
    Posts:
    2,234
    The easy way would be to manually enable the component/game object once you're ready for it to run, i.e. you disable the component on the prefab (it's the checkbox at the top of it's panel in the inspector), set everything that needs to be done, then re-enable the component.

    So long as you don't call Awake, you shouldn't have any issue with the script running before you're ready for it.
     
  3. JorobusLab

    JorobusLab

    Joined:
    Jul 3, 2014
    Posts:
    78
    That's a good advice Rocko, so by doing that I hope that neither Awake() get called in B until I enable it, right?
     
  4. RockoDyne

    RockoDyne

    Joined:
    Apr 10, 2014
    Posts:
    2,234
    If I remember correctly, awake is the one that is called regardless of whether it's enabled or not.
     
  5. PrefabEvolution

    PrefabEvolution

    Joined:
    Mar 27, 2014
    Posts:
    225
    If you want to put some data to some script after Instantiate but before the script invokes Awake you can do this trick:

    Code (csharp):
    1.  
    2. public class SomeScript : MonoBehaviour
    3.     {
    4.         //Define data structure to hold initialization params.
    5.         public class Params
    6.         {
    7.             public int someInt;
    8.             public string someString;
    9.             public UnityEngine.Object someObj;
    10.         }
    11.  
    12.         //Create static field to hold temporary data
    13.         static public Params initParams;
    14.  
    15.         //Copy data from static field to instance field.
    16.         public Params instanceParams = initParams;
    17.  
    18.         void Awake()
    19.         {
    20.             Debug.Log(instanceParams.someString);//Output: "Wooohooo!"
    21.         }
    22.  
    23.         //HowTo Instantiate example
    24.         static void CreateTest()
    25.         {
    26.             //Fill params
    27.             var paramsForNewInstance = new SomeScript.Params();
    28.             initParams.someInt = 47;
    29.             initParams.someString = "Wooohooo!";
    30.             initParams.someObj = new GameObject("Test gameObject");
    31.  
    32.             //Set params to static field
    33.             SomeScript.initParams = paramsForNewInstance;
    34.  
    35.             //Instantiate.
    36.             Instantiate(Resources.Load("PrefabPath"));
    37.         }
    38.     }
    39.  
    This code on C# but i think you can port it to JS :)
     
    Last edited: Jul 14, 2014
    JorobusLab likes this.
  6. PrefabEvolution

    PrefabEvolution

    Joined:
    Mar 27, 2014
    Posts:
    225