【问题标题】:Access a float in a class, in a namespace在命名空间中访问类中的浮点数
【发布时间】:2018-03-25 12:07:46
【问题描述】:

我正在尝试从另一个脚本更改下面脚本中“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 要求请求的组件“HeadLook”派生自 MonoBehaviour 或 Component 或者是一个接口。

谢谢。

【问题讨论】:

  • 该类不是从MonoBehaviour派生的,因此您无法获取它的组件,因为它不是组件。尝试查找包含HeadLook 实例的脚本。
  • 错误所在的行是。 m_HeadLook = GetComponent();

标签: c# class unity3d namespaces


【解决方案1】:

你只需要从 monobehaviour 派生你的类:

using System;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

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

【讨论】:

  • 您应该在需要时从 MB 派生。也许是这种情况,但通常情况下,如果不需要检查员分配或引擎的任何回调(更新、UI 事件...),最好避免使用 MB,因为它会增加内存使用量。
【解决方案2】:

私人 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

【讨论】:

    【解决方案3】:

    错误提示:“GetComponent 要求请求的组件 'HeadLook' 派生自 MonoBehaviour

    可能的修复:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      • 2010-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多