Unity3D--day03

GameObject.GetComponent
GetComponent(type: Type): Component;
Parameters

type The type of Component to retrieve.
Description

Returns the component of Type type if the game object has one attached, null if it doesn't. You can access both builtin components or scripts with this function.

GetComponent is the primary way of accessing other components. From javascript the type of a script is always the name of the script as seen in the project view. Example:
GetComponent的主要方式是访问其他组件。从javascript脚本的类型总是脚本的名称作为项目视图中看到。示例:
	function Start () {
		var curTransform : Transform;
		
		curTransform = gameObject.GetComponent(Transform);
		// This is equivalent to:
		curTransform = gameObject.transform;
	}

function Update () { // To access public variables and functions // in another script attached to the same game object. // (ScriptName is the name of the javascript file) var other : ScriptName = gameObject.GetComponent(ScriptName); // Call the function DoSomething on the script other.DoSomething (); // set another variable in the other script instance other.someVariable = 5; }
GetComponent(): T;
Description

Generic version. See the Generic Functions page for more details.

GetComponent(type: string): Component;
Parameters

type The type of Component to retrieve.
Description

Returns the component with name type if the game object has one attached, null if it doesn't.

返回组件名称类型如果游戏对象有一个附件,空如果它不。

It is better to use GetComponent with a Type instead of a string for performance reasons. Sometimes you might not be able to get to the type however, for example when trying to access a C# script from Javascript. In that case you can simply access the component by name instead of type. Example:
最好是使用GetComponent类型而不是一个字符串由于性能的原因。有时候你可能无法到达类型然而,例如当试图访问一个c#脚本从Javascript。在这种情况下你可以直接访问组件的名字相反的类型。示例:
	function Update () {
		// To access public variables and functions 
		// in another script attached to the same game object.
		// (ScriptName is the name of the javascript file)
		var other : ScriptName;
		other = gameObject.GetComponent("ScriptName");
		// Call the function DoSomething on the script
		other.DoSomething ();
		// set another variable in the other script instance
		other.someVariable = 5;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值