Search Unity

Instantiate as child

Discussion in 'Physics' started by unity_157325, Jun 8, 2018.

  1. unity_157325

    unity_157325

    Joined:
    May 22, 2018
    Posts:
    17
    I want to instantiate "s1Button" but it has to be child of "ChoiceButtonHolder".
    How do I have to do this?

    Code (CSharp):
    1. Instantiate(s1Button);
    ^This is what I have now
     
    md-creator likes this.
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    Just configure transform.parent after instantiating the object.
     
    md-creator likes this.
  3. unity_157325

    unity_157325

    Joined:
    May 22, 2018
    Posts:
    17
    But how?
     
  4. bakir-omarov

    bakir-omarov

    Joined:
    Aug 1, 2015
    Posts:
    48
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ExampleClass : MonoBehaviour
    5. {
    6.     // manually add prefab in Unity Editor
    7.     public GameObject s1Button_prefab;
    8.     private GameObject s1Button;
    9.  
    10.     // manually add in Unity Editor
    11.     public GameObject ChoiceButtonHolder;
    12.  
    13.  
    14.     private void Start()
    15.     {
    16.         s1Button = Instantiate(s1Button_prefab);
    17.         //Sets "ChoiceButtonHolder" as the new parent of the s1Button.
    18.         s1Button.transform.SetParent(ChoiceButtonHolder.transform);
    19.     }
    20. }
    Also you can read more about it :
    https://docs.unity3d.com/ScriptReference/Transform.SetParent.html
     
  5. unity_157325

    unity_157325

    Joined:
    May 22, 2018
    Posts:
    17
    I tried that but then I got this error,
    Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption.
    UnityEngine.Transform:SetParent(Transform)
     
  6. bakir-omarov

    bakir-omarov

    Joined:
    Aug 1, 2015
    Posts:
    48
    This error is coming up when you trying to change PREFAB's parent. As you can see, we are setting parent of new GameObject s1Button.transform.SetParent(ChoiceButtonHolder.transform); . If you will attempt to change s1Button_prefab parent you will get error. You can share your code if you can't fix it.
     
  7. unity_157325

    unity_157325

    Joined:
    May 22, 2018
    Posts:
    17
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ChoiceSpawner : MonoBehaviour {
    6.    
    7.     [SerializeField]
    8.     private GameObject PlayerComponents;
    9.  
    10.     [SerializeField]
    11.     private GameObject s1Button_prefab;
    12.     private GameObject s1Button;
    13.  
    14.     [SerializeField]
    15.     private GameObject s2Button;
    16.  
    17.     public GameObject ChoiceButtonHolder;
    18.  
    19.     public void Spawner(int RealNewSpell)
    20.     {
    21.  
    22.         Debug.Log(RealNewSpell);
    23.  
    24.         if (RealNewSpell == 1)
    25.         {
    26.             Debug.Log("test1");
    27.             s1Button = Instantiate(s1Button_prefab);
    28.             s1Button.transform.SetParent(ChoiceButtonHolder.transform);
    29.         }
    This is my code
     
    cloud_2017 likes this.
  8. bakir-omarov

    bakir-omarov

    Joined:
    Aug 1, 2015
    Posts:
    48
    You code is seems okay, i used it in empty project, and all is perfectly works.

    But I suspect you have a error on the ChoiceButtonHolder . Lets clarify that ChoiceButtonHolder have to be a GameObject in the scene. It seems that your ChoiceButtonHolder is also a prefab like s1Button_prefab. You can't set PREFAB as the parent. Parent have to be a GameObject in the scene. If ChoiceButtonHolder already in your scene you can find it by name or tag, and use it as a parent.
     
    unity_h36Dp8nmGLPPVA likes this.
  9. unity_157325

    unity_157325

    Joined:
    May 22, 2018
    Posts:
    17
    Ok, so I have to tag it
     
  10. unity_157325

    unity_157325

    Joined:
    May 22, 2018
    Posts:
    17
    How do i have to search it by tag or name?
     
  11. bakir-omarov

    bakir-omarov

    Joined:
    Aug 1, 2015
    Posts:
    48
    For this type questions my advice just type in google :
    "how to find gameobject by tag in unity"
    "how to find gameobject by name in unity"

    You will find much more information from forums and Unity Answers.

    Simple way of finding by tag is :
    Code (CSharp):
    1. ChoiceButtonHolder = GameObject.FindWithTag("TagNameOfChoiceButtonHolder");
    Unity Docs: https://docs.unity3d.com/ScriptReference/GameObject.FindWithTag.html
    --
    Simple way of finding by name is :
    Code (CSharp):
    1. ChoiceButtonHolder = GameObject.Find("ChoiceButtonHolderNameOnUnityHierarchy");
    Unity Docs: https://docs.unity3d.com/ScriptReference/GameObject.Find.html
     
    unity_157325 likes this.
  12. unity_157325

    unity_157325

    Joined:
    May 22, 2018
    Posts:
    17
    Thank you so much!
     
  13. unity_157325

    unity_157325

    Joined:
    May 22, 2018
    Posts:
    17
    I have another question because another scripts has to start a function in the other script but I get this error: "
    NullReferenceException: Object reference not set to an instance of an object
    NewSpellManager.NewSpellFunction () (at Assets/NewSpellManager.cs:53)"

    This is the script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class NewSpellManager : MonoBehaviour {
    6.  
    7.  
    8.     private int NewSpell;
    9.     [HideInInspector]
    10.     public int RealNewSpell;
    11.  
    12.  
    13.     public Account Account;
    14.     private GameObject ChoiceSpawner;
    15.  
    16.  
    17.     private void Start()
    18.     {
    19.         NewSpellFunction();
    20.         ChoiceSpawner = GameObject.FindWithTag("ChoiceButtonHolder");
    21.     }
    22.  
    23.  
    24.     private void NewSpellFunction()
    25.     {
    26.  
    27.         NewSpell = Random.Range(1, Account.HowManySpells += 1);
    28.  
    29.  
    30.         if(NewSpell == 1)
    31.         {
    32.             RealNewSpell = Account.n1;
    33.         }
    34.         if (NewSpell == 2)
    35.         {
    36.             RealNewSpell = Account.n2;
    37.         }
    38.         if (NewSpell == 3)
    39.         {
    40.             RealNewSpell = Account.n3;
    41.         }
    42.         if (NewSpell == 4)
    43.         {
    44.             RealNewSpell = Account.n4;
    45.         }
    46.         if (NewSpell == 5)
    47.         {
    48.             RealNewSpell = Account.n5;
    49.         }
    50.  
    51.         Debug.Log(RealNewSpell);
    52.         Debug.Log(NewSpell);
    53.         ChoiceSpawner.GetComponent<ChoiceSpawner>().Spawner(RealNewSpell);
    54.  
    55.     }
    56.  
    57. }
    58.  
     
    md-creator likes this.
  14. bakir-omarov

    bakir-omarov

    Joined:
    Aug 1, 2015
    Posts:
    48
    In c# your scripts is reading line by line. In Start method you are calling ChoiceSpawner from NewSpellFunction(); (line 53). But you have not Find it yet. So Unity says that "Dude i cant find your ChoiceSpawner" . Firstly find it, and then you can call it when you want.

    Code (CSharp):
    1.   private void Start()
    2.     {
    3.        ChoiceSpawner = GameObject.FindWithTag("ChoiceButtonHolder");
    4.         NewSpellFunction();
    5.     }
     
    md-creator and unity_157325 like this.
  15. unity_157325

    unity_157325

    Joined:
    May 22, 2018
    Posts:
    17
    Oh omg I am so dumb
     
  16. unity_157325

    unity_157325

    Joined:
    May 22, 2018
    Posts:
    17
    I have one last question, if I instantiated that object how can I change the position, rotation and the scale?
     
    md-creator likes this.
  17. bakir-omarov

    bakir-omarov

    Joined:
    Aug 1, 2015
    Posts:
    48
    Unity has a good Documentation : https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

    Here you can see the line:

    Code (CSharp):
    1. Instantiate(Object original, Vector3 position, Quaternion rotation, Transform parent);
    You can set position, rotation and also a parent with instantiate method.

    You can scale GameObject by using localScale : https://docs.unity3d.com/ScriptReference/Transform-localScale.html


    Code (CSharp):
    1.  void Example()
    2.     {
    3.         // Widen the object by 0.1
    4.         s1Button.transform.localScale += new Vector3(0.1F, 0, 0);
    5.     }
     
    md-creator and HosseinArabbeigi like this.
  18. unity_157325

    unity_157325

    Joined:
    May 22, 2018
    Posts:
    17
    Thank you :)
     
    md-creator likes this.