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:

   Controlling GameObjects Using Components

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

  Controlling GameObjects Using Components

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

  Controlling GameObjects Using Components
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:
  Controlling GameObjects Using Components
  An object or a collection of objects can also be located by their tag using the GameObject.FindWithTag and GameObject.FindGameObjectsWithTag functions:
 
  Controlling GameObjects Using Components
 
   

Controlling GameObjects Using Components

上一篇:Windows驱动开发-IRP结构体


下一篇:《C# GDI+ 破境之道》:第一境 GDI+基础 —— 第三节:画圆形