Unity 2: GetComponent Command

David Joiner, Kean University

One of the most important features of Unity’s scripting language is the ‘GetComponen’ command. Each game object in Unity has its features extended by components. Components can be used to attach a renderer, or a collider, or a special effect. Components can also be used to attach our custom scripts.

At times, you will want a script on an object to be able to access that object’s other components. Sometimes, you will want your script on one gameobject to be able to access public member variables and methods of a script attached to another game object. GetComponent is the plumbing that will allow that to happen.

This posting (https://joinerda.github.io/GameObject-Communication) will introduce you to the GetComponent command by having our oscillator from the first blog not just oscillate around some equilibrium, but around some equilibrium bond distance to another object.

Additionally, we will show another feature of the Unity editor, which is the use of the Unity editor to set the values of public member variables in a scene, as well as the creation of prefab objects to make it easy to create many objects with the same customizations.

These features of Unity will be combined to create a chain of oscillators bound to each other by spring-like forces.

This blog post assumes you have worked through the previous post, or that you know how to add a gameobject to a unity scene and attach a script to the object.

Unity 1 post | Unity 3 post

Leave a Reply