我試圖在下面的腳本中從另一個腳本更改'XSensitivity'的值。訪問名稱空間中的類中的float浮點數
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要求被請求的組件從MonoBehaviour或組件「HeadLook」起源或接口。
謝謝。
該類不是從'MonoBehaviour'派生的,因此不能得到它的組件,因爲它不是組件。嘗試找到包含「HeadLook」實例的腳本。 –
錯誤所在的行是。 m_HeadLook = GetComponent < HeadLook >(); –