3

对于学校任务,我必须制作一个基本的视频游戏。我正在尝试制作太空游戏的基本版本。

我想要一个包含多个天空盒的数组,并且在启动游戏时,我希望游戏从数组中选择一个随机的天空盒。这样你就会有一种感觉,你每次都在不同的重生点。

谁能帮我?我一直在谷歌上寻找,但我找不到任何有用的东西。

天空盒位于一个名为:

  • 'Assets/SkyBox Volume 2/DeepSpaceBlue' 文件名 = DSB
  • 'Assets/SkyBox Volume 2/DeepSpaceGreen' 文件名 = DSG
  • ...

这是我当前的代码,它包含错误。

编辑

using UnityEngine;
using System.Collections;
public class RandomSkybox : MonoBehaviour
{
    public Material[] materials;

    // Use this for initialization
    void Start()
    {
        skyBoxMaterial = materials[Random.Range(0, materials.length)];
        RenderSettings.skybox = skyBoxMaterial;
    }

    // Update is called once per frame
    void Update()
    {

    }
}

错误:

Severity    Code    Description Project File    Line    Suppression State
Error   CS0103  The name 'skyBoxMaterial' does not exist in the current context SpaceRaiders.CSharp D:\Documenten\Unity\SpaceRaiders\Assets\Scripts\Space\RandomSkybox.cs   10  Active
Error   CS1061  'Material[]' does not contain a definition for 'length' and no extension method 'length' accepting a first argument of type 'Material[]' could be found (are you missing a using directive or an assembly reference?)   SpaceRaiders.CSharp D:\Documenten\Unity\SpaceRaiders\Assets\Scripts\Space\RandomSkybox.cs   10  Active
Error   CS0103  The name 'skyBoxMaterial' does not exist in the current context SpaceRaiders.CSharp D:\Documenten\Unity\SpaceRaiders\Assets\Scripts\Space\RandomSkybox.cs   11  Active
4

1 回答 1

4
public Skybox[] skyBoxes;

你必须

将项目拖到数组中

就这么简单。在Inspector中,将长度设置为“5”,然后将五个项目拖入。仅此而已。


同样在您发布的最新版本中,

skyBoxMaterial = materials[Random.Range(0, materials.length)];

你忘了声明变量。就这么简单。

Material skyBoxMaterial;
skyBoxMaterial = materials[Random.Range(0, materials.length)];

对于“长度”,显然是“长度”。不要忘记 List<> 它.Count不是.Length.

于 2016-03-06T18:37:25.860 回答