iT邦幫忙

2021 iThome 鐵人賽

DAY 24
0
Software Development

從零開始了解Unity或Unreal Engine應用於VR軟體開發系列 第 24

Day 24. VR菜單2

昨天打完程式碼後,出現下面的Bug:

Assets/LineRendererSetting.cs(5,22): error CS1514: { expected
Assets/LineRendererSetting.cs(5,22): error CS1513: } expected
Assets/LineRendererSetting.cs(5,22): error CS1022: Type or namespace definition, or end-of-file expected
Assets/LineRendererSetting.cs(5,23): error CS0116: A namespace cannot directly contain members such as fields or methods
Assets/LineRendererSetting.cs(5,32): error CS1022: Type or namespace definition, or end-of-file expected
Assets/LineRendererSetting.cs(5,33): error CS0116: A namespace cannot directly contain members such as fields or methods
Assets/LineRendererSetting.cs(5,50): error CS1022: Type or namespace definition, or end-of-file expected
Assets/LineRendererSetting.cs(5,52): error CS0116: A namespace cannot directly contain members such as fields or methods
Assets/LineRendererSetting.cs(6,1): error CS1022: Type or namespace definition, or end-of-file expected
Assets/LineRendererSetting.cs(32,1): error CS1022: Type or namespace definition, or end-of-file expected

沒get到是什麼意思,找了好久,才發現說原來是因為Unity創建c# script時,會直接把class名稱跟檔案名同名,但我一開始建立時有寫錯檔案名,後來才改檔名的,但是class的名稱還是保持舊的名字,所以就發生以上錯誤。

public class 與C# script檔名相同 : MonoBehaviour

更新:
好像跟class名無關,不過我找到另外的錯誤了,應該是要像這樣宣告:

//LineRendererSettings.cs
public class NewBehaviourScript : MonoBehaviour
{
    ...
    
    
    public LayerMask layerMask;

    public void AlignLineRenderer(LineRenderer rend)
    {
        ...
    }

}

我一開始宣告成這樣:

//LineRendererSettings.cs
public class NewBehaviourScript : MonoBehaviour
{
    ...
    
    
    

}

public LayerMask layerMask;

public void AlignLineRenderer(LineRenderer rend)
{
    ...
}

更新心得:

笑死,我昨天打這篇一直打瞌睡,一行字就花了我15分鐘,不知道在寫什麼。

參考:
https://learn.unity.com/tutorial/creating-a-vr-menu-2019-2#6036dc27edbc2a50f848a701


上一篇
Day 23. VR菜單
下一篇
Day 25. VR菜單3
系列文
從零開始了解Unity或Unreal Engine應用於VR軟體開發31

尚未有邦友留言

立即登入留言