Exemple #1
0
        // Use this for initialization
        public void Start()
        {
            Debug.Log("PRINT STUFF: Ready to print stuff.");

            /**
             * We need to store a reference to the WriteStuff
             * instance in the scene.
             *
             * In this example, instead of calling a method in
             * the code, I serialize the field and then attach
             * a GameObject to the variable in the Unity inspector.
             **/

            if (gameObject != null)
            {
                Debug.Log("PRINT STUFF: Looking to see if WriteStuff is attached to this game object.");
                writeStuff = gameObject.GetComponent <WriteStuff>();

                if (writeStuff != null)
                {
                    Debug.Log("PRINT STUFF: Hooray, we found WriteStuff.");
                }
                else
                {
                    Debug.LogError("PRINT STUFF: Uh oh, we didn't find WriteSuff.  Can you check it is attached to the game object please");
                }
            }
            else
            {
                Debug.LogError("PRINT STUFF: No GameObject attached in inspector.");
            }
        }
        // Use this for initialization
        public void Start()
        {
            Debug.Log("PRINT STUFF: Ready to print stuff.");

            /**
             * We need to store a reference to the WriteStuff
             * instance in the scene but need the GameObject
             * it is attached to.
             *
             * I can do this by using multiple approaches, including:
             *
             * 1) Finding a specific GameObject by name.
             * 2) Finding a game object by tag.
             *
             * After that, I can then use GetComponent<T>.
             **/
            GameObject gameObject;

            switch (gameObjectSearchMethod)
            {
            case Method.FIND_BY_NAME:
                gameObject = GameObject.Find(gameObjectName);
                break;

            case Method.FIND_BY_TAG:
                gameObject = GameObject.FindGameObjectWithTag(gameObjectTag);
                break;

            default:
                gameObject = null;
                break;
            }

            if (gameObject != null)
            {
                Debug.Log("PRINT STUFF: Looking to see if WriteStuff is attached to the game object we found.");
                writeStuff = gameObject.GetComponent <WriteStuff>();

                if (writeStuff != null)
                {
                    Debug.Log("PRINT STUFF: Hooray, we found WriteStuff.");
                }
                else
                {
                    Debug.LogError("PRINT STUFF: Uh oh, we didn't find WriteSuff.  Can you check it is attached to the game object please");
                }
            }
            else
            {
                Debug.LogError("PRINT STUFF: We couldn't find the GameObject in the scene.");
            }
        }
        // Use this for initialization
        public void Start()
        {
            Debug.Log("PRINT STUFF: Ready to print stuff.");

            /**
             * I'm using the GetComponent<T> method as before, but in
             * my Example1Broken scene, the WriteStuff component simply
             * isn't there, so this value will store null.
             **/

            Debug.Log("PRINT STUFF: Looking to see if WriteStuff is attached to this game object.");
            writeStuff = this.GetComponent <WriteStuff>();
        }