Controlling GameObjects Using Components

Accessing Components

  The most common case is where a script needs access to other Components attached to the same GameObject.A Component is actually an instance of a class so the first step is to get a reference to the Component instance you want to work with. This is done with theGetComponent function. Typically, you want to assign the Component object to a variable, which is done in C# using the following syntax:

   

  Once you have a reference to a Component instance, you can set the values of its properties much as you would in the Inspector:

  

  An extra feature that is not available in the Inspector is the possibility of calling functions on Component instances:

  
Finding Objects by Name or Tag
  It is always possible to locate GameObjects anywhere in the scene hierarchy as long as you have some information to identify them. Individual objects can be retrieved by name using the GameObject.Find function:
  
  An object or a collection of objects can also be located by their tag using the GameObject.FindWithTag and GameObject.FindGameObjectsWithTag functions:
 
  
 
   
原文地址:https://www.cnblogs.com/tekkaman/p/3536828.html