Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question instantiate a object with a constructor

Discussion in 'Scripting' started by Programer_YEAA, Jul 28, 2022.

  1. Programer_YEAA

    Programer_YEAA

    Joined:
    Jun 3, 2022
    Posts:
    91
    I want to instantiate a object who has a constructor. I read this two thread (https://answers.unity.com/questions/1826972/how-can-i-instantiate-a-object-with-a-constructor.html) (https://forum.unity.com/threads/instantiate-with-constructor-is-it-possible.325347/) but my case is diferent.
    This is my code
    Code (CSharp):
    1. newElementBody = Instantiate(
    2.     elementBody,
    3.     controller.GetWorldPositionLeft(),
    4.     Quaternion.identity,
    5.     transform
    6. );
    7.  
    8. newElementBody.name = "Element Body";
    Code (CSharp):
    1. public class ElementBody : MonoBehaviour
    2. {
    3.     private ElementBody target;
    4.  
    5.     ElementBody(ElementBody target)
    6.     {
    7.         this.target = target;
    8.     }
    9. }
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,647
    You can't use constructors with monobehaviours as their life time doesn't follow the same rules of non unity objects, given they only exist attached to game objects.

    Instead of a constructor, just give it your own 'Initialise' method and call that after you get the return on the Instantiate.
     
  3. Programer_YEAA

    Programer_YEAA

    Joined:
    Jun 3, 2022
    Posts:
    91
    But how can I reach the method? I can't do newElementBody.Initiate(); becuse newElementBody is a gameObject and not a ElementBody. If I hange that the prefab wont work, and that needs to be a gameObject.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,647
    Please try and listen to what myself and others are saying.

    Particularly this part in bold:
    Firstly, Monobehaviours are not game objects. They are components on game objects. They live, breathe and die with game objects they are attached to. You can only create or remove them via the inspector, or with AddComponent or RemoveComponent, no exceptions. I already told you in the last thread that you can't instantiate monobehaviours with constructors.

    Secondly, you can serialise references to other components, including if said component is on the root game object of a prefab. If you Instantiate this reference, it will copy the game object this component is attached to and anything else with it. You also get the copy returned via the method.

    You were already given a perfect example on how to achieve what you want in the previous thread.
     
    Kurt-Dekker likes this.
  5. Programer_YEAA

    Programer_YEAA

    Joined:
    Jun 3, 2022
    Posts:
    91
    Ah, sorry for not reading it all. But it works now. Thx :)