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

Instantiating Random Nested Prefabs at Run Time

Discussion in 'Scripting' started by lordcacops, Jul 8, 2021.

  1. lordcacops

    lordcacops

    Joined:
    Nov 26, 2020
    Posts:
    3
    Howdy.


    I have a question, and wanted to see if there’s and answer for it. I want to know how to instantiate prefabs as children of other prefabs. In particular, I am interested in creating complex game objects assembled from different parts, as prefabs, where the parent on which those prefabs are nested is a prefab as well. Imagine a system in which “animals” are created by some command, like pressing spacebar for instance. These animals, whose bodies are capsules, have attached to them some additional object chosen at random from a limited set of possibilities.


    It could choose, for example, between a blue cube, a yellow sphere, or a green cylinder. At the animal’s birth, whenever the user presses the spacebar, one of these three possibilities is chosen at random. The animal then is born as a capsule with one of these polygons attached. (I want to know the programming)...

    PARENTHETICAL EDIT: I want to know if there exists any typical subroutines that are used for cases such as the one that I am currently describing:

    (recap)
    to instantiate a prefab (the animal’s random annexed 3D object) as a child of another prefab (the animal’s core body). Moreover, I need the child prefab to be attached to the animal at birth, and to remain attached to the animal thereon, remaining stuck to it, since it is part of the animal’s body.


    This is for a multi-agent ecosystem simulation, in case you are wondering. The instantiation of the animal itself is coded in a script attached to a “Flock” object. The instantiation of the random parts over it should hopefully be found in a script attached to the animal, that I called “Genotype”.


    Thank you so much.
     
    Last edited: Jul 9, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,969
    Parental relationships between GameObject area all tracked in the Transform component.

    To change that relationship, use
    Transform.SetParent();
    on the child.

    To change the relative offset or rotation, access the .localPosition and .localRotation on the child.

    Instantiate<T>()
    also supports overloads that take an optional Transform as the parent to immediately parent the freshly-created item to. This is critical to use when dealing with UI, for instance, but may be helpful to you as well.

    "knowing the programming needed" isn't really a thing as such, kinda like you can't just "know the controls needed" in order to fly a 747 around the world.

    Here are some great tutorials to get you a leg up and save a lot of time.

    Imphenzia / imphenzia - super-basic Unity tutorial:



    Jason Weimann:



    Brackeys super-basic Unity Tutorial series:



    Sebastian Lague Intro to Game Development with Unity and C#:



    Imphenzia: How Did I Learn To Make Games:



    How to do tutorials properly:

    Tutorials are a GREAT idea. Tutorials should be used this way:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly. Fortunately this is the easiest part to get right. Be a robot. Don't make any mistakes. BE PERFECT IN EVERYTHING YOU DO HERE.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    If you want a small pile of example procedural generation stuff, check out my MakeGeo repository.

    MakeGeo is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/makegeo

    https://github.com/kurtdekker/makegeo

    https://gitlab.com/kurtdekker/makegeo

    https://sourceforge.net/p/makegeo
     
  3. lordcacops

    lordcacops

    Joined:
    Nov 26, 2020
    Posts:
    3
    Thank you for the information at the beginning of your reply.

    Transform.SetParent(); seems not to work. The following error message appears: "Setting the parent of a transform which resides in a Prefab Asset is disabled to prevent data corruption." This is problematic since I need both parent and child to be prefabs.
     
    Last edited: Jul 9, 2021