Unity API Parsing (7)--object class

Source: Internet
Author: User
Tags unique id

The object class is the base class for all objects in unity

Getinstanceid--object Object ID

Each object has a unique ID in the project and runs from the program to the end, unless the object is destroyed, and the corresponding ID for each instance does not change

Each Gameobject object with the same name created or cloned from gameobject.createprimitive () or object.instantiate () has a unique ID

Using unityengine;using System.collections;public class Getinstanceid_ts:monobehaviour {void Start () {        Debug.Log ( "Gameobject ID" +gameobject.getinstanceid ());        Debug.Log ("Transform ID" +transform. Getinstanceid ());        Gameobject G1, G2;        Create an object from Gameobject        g1=gameobject.createprimitive (primitivetype.cube);        Clone Object        g2=instantiate (g1,vector3.zero,quaternion.identity) as Gameobject;        Debug.Log ("Gameobject G1 ID" +g1. Getinstanceid ());        Debug.Log ("Transform G1 ID" +g1.transform.getinstanceid ());        Debug.Log ("Gameobject G2 ID" + G2. Getinstanceid ());        Debug.Log ("Transform G2 ID" + G2.transform.GetInstanceID ());}}

Destroy Method-Destroying objects

public static void Destroy (Object obj, float t)

Destroy an object (either an object or a component) after executing this method for t seconds

Dontdestroyonload Method-Preserving objects in a new scene

If Target is a Gameobject object of the root object or a component in the Gameobject object, the object itself and its sub-objects are imported into the new scene

If you want to import a sub-object into a new scene, you can use the Transform.detachchildren method to separate the parent-child hierarchy, and then import the new scene

Findobjectoftype Method--Get Object

Gets all objects in the project that conform to the parameter type, this method needs to traverse the entire project, the execution is slow, not suitable to call in each frame, for the result of the traversal can be selected by the name or Instanceid of the object

findobjectoftype--gets the first object in the project that conforms to type types, and is used to detect whether a project contains some type of object

Using unityengine;using System.collections;public class Findobjectoftype_ts:monobehaviour {void Start () {        gameobject[] Gos = Findobjectsoftype (typeof (Gameobject)) as gameobject[];        foreach (Gameobject go in Gos) {            ///1.5 seconds after destroying all Gameobject if except camera            (go.name! = "Main camera")            {                Destroy (go , 1.5f);            }        }        rigidbody[] RBS = Findobjectsoftype (typeof (Rigidbody)) as rigidbody[];        foreach (Rigidbody RB in RBS) {            //enable gravity sensing for all rigid bodies except spheres            (rb.name!= "Sphere") {                rb.usegravity = true;            }        }}}

instantiate--instantiating an Object

When an object is instantiated, all subclasses of the root object are instantiated at the same time

Unity API Parsing (7)--object class

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.