Time for action – communicating with another Component on the Main Camera

Let's create another script with a variable and a method, and attach it to the Main Camera, then have LearningScript communicate with it:

  1. In Unity, create another C# Script and name it TalkToMe.
  2. Make a public string variable named hereItIs.
  3. Assign some text to hereItIs.
  4. Make a public method named MakeMeTalk().
  5. Have MakeMeTalk() output some text to the Console.
  6. Attach MakeMeTalk() to the Main Camera. Now the code should look something like this:
    Time for action – communicating with another Component on the Main Camera
  7. Modify LearningScript to retrieve the TalkToMe Component.
  8. Modify LearningScript to retrieve the data in hereItIs.
  9. Modify LearningScript to call the MakeMeTalk() method. Now the code snippet should look as follows:
    Time for action – communicating with another Component on the Main Camera
  10. Save your scripts.
  11. Click on Play in Unity.

What just happened?

Here's the output:

What just happened?

The LearningScript Component code retrieved a variable and called a method on the TalkToMe Component. Let's follow the code flow with these two Components.

An analysis of the code shown in the previous code screenshot is as follows:

On LearningScript:

Line 6: TalkToMe otherComponent;

  • A variable otherComponent is declared to store a value of type TalkToMe
  • A TalkToMe Component object will be created and stored in the variable otherComponent

Line 10: otherComponent = GetComponent<TalkToMe>();

  • Remember, this is in the Start() method which Unity calls only once to initialize variables.
  • The generic version of the GetComponent<T>() method is called to retrieve a reference to the TalkToMe Component object. This is item 3 on the previous graphic under the section, Accessing another Component on the current GameObject.
  • The <T> part is the type of Component, the class name, that the method will return.
  • This reference is stored in the variable otherComponent. Why?So that every time we need to use the TalkToMe Component with Dot Syntax, we can just use the reference stored in otherComponent instead of having to use GetComponent<TalkToMe>() each time.

Line 19: Debug.Log("This is the TalkToMe Component: " + otherComponent);

  • This line of code sends the value stored in otherComponent to the Unity Console so we can see the reference that's pointing to the TalkToMe Component object

Line 20: Debug.Log(otherComponent.hereItIs);

  • Dot Syntax is used to locate and retrieve the value stored in the variable hereItIs of the TalkToMe Component object. This is item 4 in the graphic under the section, Accessing another Component on the current GameObject.
  • The hereItIs variable is declared on line 6 of the TalkToMe class.
  • Notice that hereItIs is public so that it can be accessed from other scripts.
  • If we didn't use otherComponent, we would have written the Dot Syntax expression like the following line of code:
    GetComponent<TalkToMe>().hereItIs
  • The following is a screenshot of the Scripting Reference example:

    Tip

    At the time of this writing, Unity was updating its documentation. The page was not complete. The following screenshot is the old page; however, the code is still valid.

    What just happened?

Line 21: otherComponent.MakeMeTalk();

  • Dot Syntax is used to locate and call the MakeMeTalk() method of the TalkToMe Component object
  • Code flow now jumps over to the TalkToMe class. This is also item 4 in the graphic under the section, Accessing another Component on the current GameObject.

On TalkToMe:

Line 8: public void MakeMeTalk()

  • The MakeMeTalk() method is public so that it can be called from other scripts
  • Its code block simply sends a string of text to the Unity Console
  • The code block ends and code flow returns to the LearningScript class

On LearningScript:

Line 22: }

  • Code flow has now reached the end of the if statement which began on line 17, and is waiting to detect if we press the Return key again

Tip

Before you proceed further with the next section, remove the TalkToMe Component from the Main Camera. We are done with this script so there's no sense in having any of its Components hanging around.

Accessing other GameObjects and their Components

You just learned to access Components on the same GameObject. Now it's time to access other Gameobjects, and their Components using Dot Syntax.

Accessing other GameObjects and their Components
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.35.72