unity3d中从组件获取gameObject

private void OnTriggerEnter(Collider other)
{
    Destroy(other.transform.gameObject);
}

很明显,我是想从触发器函数里面摧毁传入这个other,这个问题倒是很简单,但是可以从这个问题中学习到其它的东西:

Unity里面的Transform 和 GameObject是真的像,感觉你能用的我也能用,你有的我都有,这里呢就是Transform对 GameObject说的啦:
因为Transform下的方法要比GameObject多,使用频率也要高很多。
这里写图片描述
我们可以通过parent访问父节点,通过gameObject访问object,我在 class Transform中没有找到gameObject,后来才发现,gameObject在component里面:
这里写图片描述
第一个便是了

下面是Transform的代码:

//
// 摘要:
//     Position, rotation and scale of an object.
public class Transform : Component, IEnumerable
{
    protected Transform();

    //
    // 摘要:
    //     Position of the transform relative to the parent transform.
    public Vector3 localPosition { get; set; }
    //
    // 摘要:
    //     The rotation as Euler angles in degrees.
    public Vector3 eulerAngles { get; set; }
    //
    // 摘要:
    //     The rotation as Euler angles in degrees relative to the parent transform's rotation.
    public Vector3 localEulerAngles { get; set; }
    //
    // 摘要:
    //     The red axis of the transform in world space.
    public Vector3 right { get; set; }
    //
    // 摘要:
    //     The green axis of the transform in world space.
    public Vector3 up { get; set; }
    //
    // 摘要:
    //     The blue axis of the transform in world space.
    public Vector3 forward { get; set; }
    //
    // 摘要:
    //     The rotation of the transform in world space stored as a Quaternion.
    public Quaternion rotation { get; set; }
    //
    // 摘要:
    //     The position of the transform in world space.
    public Vector3 position { get; set; }
    //
    // 摘要:
    //     The rotation of the transform relative to the parent transform's rotation.
    public Quaternion localRotation { get; set; }
    //
    // 摘要:
    //     The parent of the transform.
    public Transform parent { get; set; }
    //
    // 摘要:
    //     Matrix that transforms a point from world space into local space (Read Only).
    public Matrix4x4 worldToLocalMatrix { get; }
    //
    // 摘要:
    //     Matrix that transforms a point from local space into world space (Read Only).
    public Matrix4x4 localToWorldMatrix { get; }
    //
    // 摘要:
    //     Returns the topmost transform in the hierarchy.
    public Transform root { get; }
    //
    // 摘要:
    //     The number of children the Transform has.
    public int childCount { get; }
    //
    // 摘要:
    //     The global scale of the object (Read Only).
    public Vector3 lossyScale { get; }
    //
    // 摘要:
    //     Has the transform changed since the last time the flag was set to 'false'?
    public bool hasChanged { get; set; }
    //
    // 摘要:
    //     The scale of the transform relative to the parent.
    public Vector3 localScale { get; set; }
    //
    // 摘要:
    //     The transform capacity of the transform's hierarchy data structure.
    public int hierarchyCapacity { get; set; }
    //
    // 摘要:
    //     The number of transforms in the transform's hierarchy data structure.
    public int hierarchyCount { get; }

    //
    // 摘要:
    //     Unparents all children.
    [GeneratedByOldBindingsGenerator]
    public void DetachChildren();
    //
    // 摘要:
    //     Finds a child by n and returns it.
    //
    // 参数:
    //   n:
    //     Name of child to be found.
    //
    //   name:
    //
    // 返回结果:
    //     The returned child transform or null if no child is found.
    [GeneratedByOldBindingsGenerator]
    public Transform Find(string name);
    [Obsolete("FindChild has been deprecated. Use Find instead (UnityUpgradable) -> Find([mscorlib] System.String)", false)]
    public Transform FindChild(string name);
    //
    // 摘要:
    //     Returns a transform child by index.
    //
    // 参数:
    //   index:
    //     Index of the child transform to return. Must be smaller than Transform.childCount.
    //
    // 返回结果:
    //     Transform child by index.
    [GeneratedByOldBindingsGenerator]
    public Transform GetChild(int index);
    [GeneratedByOldBindingsGenerator]
    [Obsolete("use Transform.childCount instead.")]
    public int GetChildCount();
    public IEnumerator GetEnumerator();
    //
    // 摘要:
    //     Gets the sibling index.
    [GeneratedByOldBindingsGenerator]
    public int GetSiblingIndex();
    //
    // 摘要:
    //     Transforms a direction from world space to local space. The opposite of Transform.TransformDirection.
    //
    // 参数:
    //   direction:
    public Vector3 InverseTransformDirection(Vector3 direction);
    //
    // 摘要:
    //     Transforms the direction x, y, z from world space to local space. The opposite
    //     of Transform.TransformDirection.
    //
    // 参数:
    //   x:
    //
    //   y:
    //
    //   z:
    public Vector3 InverseTransformDirection(float x, float y, float z);
    //
    // 摘要:
    //     Transforms the position x, y, z from world space to local space. The opposite
    //     of Transform.TransformPoint.
    //
    // 参数:
    //   x:
    //
    //   y:
    //
    //   z:
    public Vector3 InverseTransformPoint(float x, float y, float z);
    //
    // 摘要:
    //     Transforms position from world space to local space.
    //
    // 参数:
    //   position:
    public Vector3 InverseTransformPoint(Vector3 position);
    //
    // 摘要:
    //     Transforms a vector from world space to local space. The opposite of Transform.TransformVector.
    //
    // 参数:
    //   vector:
    public Vector3 InverseTransformVector(Vector3 vector);
    //
    // 摘要:
    //     Transforms the vector x, y, z from world space to local space. The opposite of
    //     Transform.TransformVector.
    //
    // 参数:
    //   x:
    //
    //   y:
    //
    //   z:
    public Vector3 InverseTransformVector(float x, float y, float z);
    //
    // 摘要:
    //     Is this transform a child of parent?
    //
    // 参数:
    //   parent:
    [GeneratedByOldBindingsGenerator]
    public bool IsChildOf(Transform parent);
    //
    // 摘要:
    //     Rotates the transform so the forward vector points at target's current position.
    //
    // 参数:
    //   target:
    //     Object to point towards.
    //
    //   worldUp:
    //     Vector specifying the upward direction.
    [ExcludeFromDocs]
    public void LookAt(Transform target);
    //
    // 摘要:
    //     Rotates the transform so the forward vector points at worldPosition.
    //
    // 参数:
    //   worldPosition:
    //     Point to look at.
    //
    //   worldUp:
    //     Vector specifying the upward direction.
    public void LookAt(Vector3 worldPosition, [DefaultValue("Vector3.up")] Vector3 worldUp);
    //
    // 摘要:
    //     Rotates the transform so the forward vector points at worldPosition.
    //
    // 参数:
    //   worldPosition:
    //     Point to look at.
    //
    //   worldUp:
    //     Vector specifying the upward direction.
    [ExcludeFromDocs]
    public void LookAt(Vector3 worldPosition);
    //
    // 摘要:
    //     Rotates the transform so the forward vector points at target's current position.
    //
    // 参数:
    //   target:
    //     Object to point towards.
    //
    //   worldUp:
    //     Vector specifying the upward direction.
    public void LookAt(Transform target, [DefaultValue("Vector3.up")] Vector3 worldUp);
    //
    // 摘要:
    //     Applies a rotation of zAngle degrees around the z axis, xAngle degrees around
    //     the x axis, and yAngle degrees around the y axis (in that order).
    //
    // 参数:
    //   xAngle:
    //     Degrees to rotate around the X axis.
    //
    //   yAngle:
    //     Degrees to rotate around the Y axis.
    //
    //   zAngle:
    //     Degrees to rotate around the Z axis.
    //
    //   relativeTo:
    //     Rotation is local to object or World.
    public void Rotate(float xAngle, float yAngle, float zAngle, [DefaultValue("Space.Self")] Space relativeTo);
    [ExcludeFromDocs]
    public void Rotate(Vector3 eulerAngles);
    //
    // 摘要:
    //     Applies a rotation of eulerAngles.z degrees around the z axis, eulerAngles.x
    //     degrees around the x axis, and eulerAngles.y degrees around the y axis (in that
    //     order).
    //
    // 参数:
    //   relativeTo:
    //     Rotation is local to object or World.
    //
    //   eulers:
    //     Rotation to apply.
    //
    //   eulerAngles:
    public void Rotate(Vector3 eulerAngles, [DefaultValue("Space.Self")] Space relativeTo);
    [ExcludeFromDocs]
    public void Rotate(float xAngle, float yAngle, float zAngle);
    [ExcludeFromDocs]
    public void Rotate(Vector3 axis, float angle);
    //
    // 摘要:
    //     Rotates the object around axis by angle degrees.
    //
    // 参数:
    //   axis:
    //     Axis to apply rotation to.
    //
    //   angle:
    //     Degrees to rotation to apply.
    //
    //   relativeTo:
    //     Rotation is local to object or World.
    public void Rotate(Vector3 axis, float angle, [DefaultValue("Space.Self")] Space relativeTo);
    //
    // 摘要:
    //     Rotates the transform about axis passing through point in world coordinates by
    //     angle degrees.
    //
    // 参数:
    //   point:
    //
    //   axis:
    //
    //   angle:
    public void RotateAround(Vector3 point, Vector3 axis, float angle);
    //
    // 参数:
    //   axis:
    //
    //   angle:
    [Obsolete("use Transform.Rotate instead.")]
    public void RotateAround(Vector3 axis, float angle);
    [Obsolete("use Transform.Rotate instead.")]
    public void RotateAroundLocal(Vector3 axis, float angle);
    //
    // 摘要:
    //     Move the transform to the start of the local transform list.
    [GeneratedByOldBindingsGenerator]
    public void SetAsFirstSibling();
    //
    // 摘要:
    //     Move the transform to the end of the local transform list.
    [GeneratedByOldBindingsGenerator]
    public void SetAsLastSibling();
    //
    // 摘要:
    //     Set the parent of the transform.
    //
    // 参数:
    //   parent:
    //     The parent Transform to use.
    //
    //   worldPositionStays:
    //     If true, the parent-relative position, scale and rotation are modified such that
    //     the object keeps the same world space position, rotation and scale as before.
    public void SetParent(Transform parent);
    //
    // 摘要:
    //     Set the parent of the transform.
    //
    // 参数:
    //   parent:
    //     The parent Transform to use.
    //
    //   worldPositionStays:
    //     If true, the parent-relative position, scale and rotation are modified such that
    //     the object keeps the same world space position, rotation and scale as before.
    [GeneratedByOldBindingsGenerator]
    public void SetParent(Transform parent, bool worldPositionStays);
    //
    // 摘要:
    //     Sets the world space position and rotation of the Transform component.
    //
    // 参数:
    //   position:
    //
    //   rotation:
    public void SetPositionAndRotation(Vector3 position, Quaternion rotation);
    //
    // 摘要:
    //     Sets the sibling index.
    //
    // 参数:
    //   index:
    //     Index to set.
    [GeneratedByOldBindingsGenerator]
    public void SetSiblingIndex(int index);
    //
    // 摘要:
    //     Transforms direction x, y, z from local space to world space.
    //
    // 参数:
    //   x:
    //
    //   y:
    //
    //   z:
    public Vector3 TransformDirection(float x, float y, float z);
    //
    // 摘要:
    //     Transforms direction from local space to world space.
    //
    // 参数:
    //   direction:
    public Vector3 TransformDirection(Vector3 direction);
    //
    // 摘要:
    //     Transforms the position x, y, z from local space to world space.
    //
    // 参数:
    //   x:
    //
    //   y:
    //
    //   z:
    public Vector3 TransformPoint(float x, float y, float z);
    //
    // 摘要:
    //     Transforms position from local space to world space.
    //
    // 参数:
    //   position:
    public Vector3 TransformPoint(Vector3 position);
    //
    // 摘要:
    //     Transforms vector x, y, z from local space to world space.
    //
    // 参数:
    //   x:
    //
    //   y:
    //
    //   z:
    public Vector3 TransformVector(float x, float y, float z);
    //
    // 摘要:
    //     Transforms vector from local space to world space.
    //
    // 参数:
    //   vector:
    public Vector3 TransformVector(Vector3 vector);
    //
    // 摘要:
    //     Moves the transform by x along the x axis, y along the y axis, and z along the
    //     z axis.
    //
    // 参数:
    //   x:
    //
    //   y:
    //
    //   z:
    //
    //   relativeTo:
    public void Translate(float x, float y, float z, [DefaultValue("Space.Self")] Space relativeTo);
    //
    // 摘要:
    //     Moves the transform by x along the x axis, y along the y axis, and z along the
    //     z axis.
    //
    // 参数:
    //   x:
    //
    //   y:
    //
    //   z:
    //
    //   relativeTo:
    [ExcludeFromDocs]
    public void Translate(float x, float y, float z);
    //
    // 摘要:
    //     Moves the transform in the direction and distance of translation.
    //
    // 参数:
    //   translation:
    //
    //   relativeTo:
    public void Translate(Vector3 translation, [DefaultValue("Space.Self")] Space relativeTo);
    //
    // 摘要:
    //     Moves the transform in the direction and distance of translation.
    //
    // 参数:
    //   translation:
    //
    //   relativeTo:
    [ExcludeFromDocs]
    public void Translate(Vector3 translation);
    //
    // 摘要:
    //     Moves the transform by x along the x axis, y along the y axis, and z along the
    //     z axis.
    //
    // 参数:
    //   x:
    //
    //   y:
    //
    //   z:
    //
    //   relativeTo:
    public void Translate(float x, float y, float z, Transform relativeTo);
    //
    // 摘要:
    //     Moves the transform in the direction and distance of translation.
    //
    // 参数:
    //   translation:
    //
    //   relativeTo:
    public void Translate(Vector3 translation, Transform relativeTo);
}

我发现真的是可以用Transform替代Gameobject。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值