在命名空间中访问类中的float

时间:2017-10-13 11:06:33

标签: c# class unity3d namespaces

我试图改变' XSensitivity'的价值。在另一个脚本的下面脚本中。

using System;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

namespace UnityStandardAssets.Characters.FirstPerson
{
[Serializable]
public class HeadLook
{
    public float XSensitivity = 8f;
    public float YSensitivity = 8f;
    public float MinimumX = -60F;
    public float MaximumX = 60F;

    public float xRotRaw;
    public float yRotRaw;

    public HeadLook (float rotX, float rotY)
    {
        rotX = XSensitivity;
        rotY = YSensitivity;
    }
    // create an instance (an Object) of the HeadLook class
    public HeadLook MyHeadLook = new HeadLook(8,8);

    private float xRot;
    private float yRot;

    private float xRotation;
    private float yRotation;

    //          ----------------------------
    public void LookRotation(Transform character, Transform head)
    {
        yRotRaw = CrossPlatformInputManager.GetAxisRaw("HorizontalLook");
        xRotRaw = CrossPlatformInputManager.GetAxisRaw("VerticalLook");

        yRot = CrossPlatformInputManager.GetAxisRaw("HorizontalLook") * XSensitivity;
        xRot = CrossPlatformInputManager.GetAxisRaw("VerticalLook") * YSensitivity;

        yRotation -= yRot * 10 * Time.deltaTime;
        yRotation = yRotation % 360;
        xRotation += xRot * 10 * Time.deltaTime;
        xRotation = Mathf.Clamp(xRotation, MinimumX, MaximumX);
        head.localEulerAngles = new Vector3(-xRotation, -0, 0);
        character.localEulerAngles = new Vector3(0, -yRotation, 0);
    }
    //          ----------------------------
}
}

我认为这可行,但我收到了错误。

    using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;

private HeadLook m_HeadLook;

void Awake ()
{
    m_HeadLook = GetComponent< HeadLook >();
    sensitivitySlider.value =  m_HeadLook.MyHeadLook.XSensitivity;
}

我得到的错误是...... ArgumentException:GetComponent要求所请求的组件&#39; HeadLook&#39;源自MonoBehaviour或Component,或者是一个接口。

感谢。

3 个答案:

答案 0 :(得分:1)

你只需要从monobehaviour派生你的班级:

using System;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

namespace UnityStandardAssets.Characters.FirstPerson
{
[Serializable]
public class HeadLook : MonoBehaviour
{
...

答案 1 :(得分:0)

私人HeadLook m_HeadLook;

void Awake ()
{
    m_HeadLook = new HeadLook(8f,8f);
    sensitivitySlider.value =  m_HeadLook.MyHeadLook.XSensitivity;
}

您不能使用GetComponent,因为它不是MonoBehaviour。

此外,您的HeadLook似乎正在创建一个非常必要的HeadLook对象。你似乎试图做一个单身人士,这更像是:

public static HeadLook MyHeadLook = null;

public HeadLook (float rotX, float rotY)
{
    if(MyHeadLook != null) {return;}
    MyHeadLook = this;
    rotX = XSensitivity;
    rotY = YSensitivity;
}

这真的很简单,但它将是单身人士的开始。

编辑:评论部分的某个人确保您正确使用单身人士。我的回答只是一个快速暗示你做错的事实,我提供了一些简单的东西让你去。 所以这里有一个链接到一个完全解释它的页面。

http://wiki.unity3d.com/index.php/Singleton

答案 2 :(得分:0)

错误说:&#34; GetComponent要求所请求的组件&#39; HeadLook&#39;来自MonoBehaviour &#34;

可能的修复:

  1. 从MonoBehavior派生HeadLook并且不为HeadLook定义构造函数(你应该通过其他方式设置rotX和rotY)
  2. 将HeadLook设置为您的调用脚本的成员,在这种情况下您不会使用GetComponent(即m_HeadLook = new HeadLook(1f,2f))