Search Unity

How do I add my own parameters for instantiating a prefab?

Discussion in 'Prefabs' started by gmfbrown, Nov 21, 2020.

  1. gmfbrown

    gmfbrown

    Joined:
    Jan 9, 2019
    Posts:
    25
    In object-oriented programming in C#, independently of Unity, you can add extra input parameters to the constructor that you can set when you instantiate a copy of the C# object.

    However, once Unity is brought into it, instantiating a copy of a C# class requires attaching the C# class to a game object, making it into a prefab, and instantiating the prefab. That instantiation is done using a pre-existing Unity function called Instantiate. The instantiate function only seems to come with three parameters, the prefab, the location, and the rotation. But I don't see any way to add any extra parameters to that.

    For instance, the project I'm working on involves a bunch of spinning levers. I want to be able to set the length, rotation speed, and starting orientation of each lever as parameters when I instantiate them. But the Instantiate function doesn't seem to let me do that. So the only way I can set the parameters is to instantiate it first and then immediately set the parameters afterwards. That doesn't sound like the best coding technique and it could probably lead to bugs down the line. Is there any way I can instantiate set those parameters as part of the instantiation instead of immediately afterwards?
     
  2. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,073
    I'm doing it like this:
    First of all my Instantiation system is quite complex so I'm describing only a basic principle.

    First create GameObject GO on scene and GO.SetActive(false) it.
    Than use this deactivated object as a parent for your instantiated prefabs.
    After instantiation your prefab is still disabled, you can make operation on it.
    When finished just change its parent to null (or specified parent).
     
    LHDi and adamgolden like this.
  3. gmfbrown

    gmfbrown

    Joined:
    Jan 9, 2019
    Posts:
    25
    I'm still not entirely clear on this. Can you show some sample code that demonstrates what it would look like to instantiate a prefab under these circumstances?
     
  4. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,073
    I'm writing from memory (not checked):
    Code (csharp):
    1.  
    2.  
    3. GameObject pgo = new GameObject();
    4. pgo.SetActive(false);
    5.  
    6. GameObject go = GameObject GameObject.Instantiate(prefab,pgo);
    7.  
    8. //----- here you can do operations on go before it is activated
    9. // for example go.GetComponent<MyScript>().Initialize(arg1,arg2).
    10. //set position and orientation etc.
    11. //
    12.  
    13. go.transform.parent = null;
    14.  
    You can store pgo for further initializations. No need to recreate it all the time.
     
  5. gmfbrown

    gmfbrown

    Joined:
    Jan 9, 2019
    Posts:
    25
    Thanks! That mostly worked. I did have to change the second parameter in Instantiate from pgo to pgo.transform but, other than that, it worked.