2017-10-13 34 views
1

我試圖在下面的腳本中從另一個腳本更改'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」起源或接口。

謝謝。

+0

該類不是從'MonoBehaviour'派生的,因此不能得到它的組件,因爲它不是組件。嘗試找到包含「HeadLook」實例的腳本。 –

+0

錯誤所在的行是。 m_HeadLook = GetComponent < HeadLook >(); –

回答

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

1

你只需從monobehaviour派生類:

using System; 
using UnityEngine; 
using UnityStandardAssets.CrossPlatformInput; 

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

你應該從需要時從MB派生。也許是在這種情況下,但通常情況下,如果不需要檢查器分配或引擎的任何回調(更新,UI事件...),最好避免MB,因爲它會增加內存使用量。 – Everts

0

錯誤說的那樣: 「GetComponent要求被請求的組件 'HeadLook' 導出從MonoBehaviour

潛在的修正:

  1. 派生從MonoBehavior HeadLook和不爲HeadLook定義構造函數(您應該通過其他方式設置rotX和rotY)
  2. 將HeadLook設置爲你的調用腳本,在這種情況下你不會使用GetComponent(即m_HeadLook = new HeadLook(1f,2f))
相關問題