static void Main()
    {
        // Simpler code to demonstrate the point
        ObjectParent parent = new ObjectChild();

        parent.Foo();     // Prints ObjectChild.Foo
    }
Пример #2
0
    /*
     * We'll assign the foundObject when
     * we find a ChildObject script on
     * a GameObject
     */

    // Update is called once per frame
    void Update()
    {
        if (foundObject == null)
        {
            Object[] objects = FindObjectsOfType(typeof(GameObject));
            // a list of all game objects in the scene

            foreach (GameObject o in objects)
            {
                Debug.Log(o);
                ObjectChild child = o.GetComponent(typeof(ObjectChild)) as ObjectChild;

                /*
                 * child is initialized as null
                 * if GetComponent returns an object
                 * then child is not null
                 */
                if (child != null)
                {
                    foundObject = o;

                    /*
                     * if child isn't null
                     * then we found an object
                     * with the ObjectChild attached
                     */
                }
            }
        }
    }