Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Instantiate in same folder

Discussion in 'Scripting' started by artistshc, Apr 18, 2023.

  1. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    79
    Hi. I was wondering if there is a way to instantiate an object in the same folder on the hierarchy. I have a folder named Items that I use for raycasting all the items in that folder. So I want to be able to instantiate in that folder because of the raycasting. Thank you.
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,882
    The Hierarchy doesn't have folders. It has GameObjects, and each GameObject can have children and parents thanks to the Transform features.

    You can call Instantiate() with a specific Transform which you want as the parent.

    https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

    Code (CSharp):
    1. GameObject myInstance = Instantiate(myPrefab,
    2.     myVector3whereIwantIt,
    3.     myQuaternonHowIwantItTurned,
    4.     myOtherObjectToBeItsParent.transform);
     
    artistshc and Kurt-Dekker like this.
  3. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    79
    Thank you very much, that helped immensely.