Unity uses scripting to add sub-objects to an object

Source: Internet
Author: User

First we create a cube, then we create a material ball for cube, and the color of the material ball is red. The color of the material ball to the cube,cube turns red.

Create an empty object (gameobject) named Container, as the name implies, this will be used as a container.

To write a script:

Using Unityengine;
Using System.Collections;

public class Giveparent:monobehaviour {public

    gameobject prefab;
    Use the this for initialization
    void Start () {
        Gameobject container = gameobject.find ("container");
        The first kind of
        //gameobject prefabinstance = Instantiate (prefab);
        PrefabInstance.transform.parent = Container.transform;
        The second type of
        gameobject PFB = resources.load ("Prefabs/cube") as Gameobject;
        Gameobject prefabinstance = Instantiate (PFB);
        PrefabInstance.transform.parent = Container.transform;


    }

    Update is called once per frame
    void Update () {

    }
}

The second is what I added later: The Prefab Cube was later placed under the Resource/prefabs directory

Scripts are mounted on container

Drag and drop the finished preform here

To run the program:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.