Component.GetComponent ()导致大量的错误

Component.GetComponent ()导致大量的错误

问题描述:

我想跟随Unity5的空间射击游戏教程,我遇到了刚体问题。Component.GetComponent ()导致大量的错误

我知道刚体已经被Component.GetComponent()取代,但我想做一个变量而不是全部输入。

我得到了使用Component.GetComponent()错误的TON,并不明白什么是错的。

这里是我的代码片段,我试图约束带夹具运动:

using UnityEngine; 
using System.Collections; 

public class PlayerController : MonoBehaviour { 

    public float speed; 
    public float xMin, zMin, xMax, zMax; 

    void FixedUpdate(){ 
     float moveHorizontal = Input.GetAxis("Horizontal"); 
     float moveVertical = Input.GetAxis("Vertical"); 

     Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical); 
     Component.GetComponent<Rigidbody>().velocity = movement*speed; 

     Component.GetComponent<Rigidbody>().position = new Vector3 
     (
      Mathf.Clamp(Component.GetComponent<Rigidbody>().position.x, xMin, xMax), 
      0.0f, 
      Mathf.Clamp(Component.GetComponent<Rigidbody>().position.z, zMin, zMax) 
     ); 
    } 
} 

,这里是错误的spitload它给了我:

Finished updating scripts/assemblies 

Some scripts have compilation errors which may prevent obsolete API usages to get updated. Obsolete API updating will continue automatically after these errors get fixed. 

Assets/Scripts/PlayerController.cs(14,27): error CS0120: An object reference is required to access non-static member `UnityEngine.Component.GetComponent(System.Type)' 

Assets/Scripts/PlayerController.cs(14,47): error CS0120: An object reference is required to access non-static member `UnityEngine.Component.GetComponent(System.Type)' 

Assets/Scripts/PlayerController.cs(18,31): error CS1502: The best overloaded method match for `UnityEngine.Mathf.Clamp(float, float, float)' has some invalid arguments 

Assets/Scripts/PlayerController.cs(18,31): error CS1503: Argument `#1' cannot convert `object' expression to type `float' 

Assets/Scripts/PlayerController.cs(20,47): error CS0120: An object reference is required to access non-static member `UnityEngine.Component.GetComponent(System.Type)' 

Assets/Scripts/PlayerController.cs(20,31): error CS1502: The best overloaded method match for `UnityEngine.Mathf.Clamp(float, float, float)' has some invalid arguments 

Assets/Scripts/PlayerController.cs(20,31): error CS1503: Argument `#1' cannot convert `object' expression to type `float' 

Assets/Scripts/PlayerController.cs(21,18): error CS1502: The best overloaded method match for `UnityEngine.Vector3.Vector3(float, float, float)' has some invalid arguments 

Assets/Scripts/PlayerController.cs(21,18): error CS1503: Argument `#1' cannot convert `object' expression to type `float' 

Assets/Scripts/PlayerController.cs(16,27): error CS0120: An object reference is required to access non-static member `UnityEngine.Component.GetComponent(System.Type)' 

我觉得我在想念一些重要的和显而易见的,因为这不是很多的代码来保证这么多的错误。

在使用非静态类函数之前,您应该先创建一个对象的实例。

在你的情况下,它最有可能是附有RigidBody组件的gameObject。下面是代码示例:

gameObject.GetComponent<RigidBody>().velocity = movement * speed; 

分别重做等串在你的代码。

+0

这个作品,我也发现this.gameObject.GetComponent和this.GetComponent的作品。 – user8363

+0

也只是GetComponent – user8363

+0

Aslo,因为你在几个地方使用相同的'RigidBody',如果你将创建一个包含这个组件的变量,那将是很好的。因此,使用'RigidBody rb = gameObjcet.GetComponent ()'或你的一个例子来获取它,然后每次你想与这个组件交互时,你都使用'rb'而不是'GetComponent ()'。 例如'rb.velocity =运动*速度'。 – Nikita