小编Kub*_*320的帖子

Random.Range 仅给出一个输出

我有个问题。每当我开始游戏时,我都会尝试随机化背景图像,但无论我重新滚动多少次,“BackgroundImageNumber”的输出始终为 1。提前致谢。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MenuBackgroundChooser : MonoBehaviour
{
    Image BackgroundImage;
    int BackgroundImageNumber;
    
    public Sprite Background1;
    public Sprite Background2;
    
    void Awake()
    {
        BackgroundImage = GetComponent<Image>();
    }
    
    void Start()
    {
        //Set the second nuber to the number of images and increase the switch when adding a background
        BackgroundImageNumber = Random.Range(1, 2);
        
        Debug.Log(BackgroundImageNumber);
        
        switch(BackgroundImageNumber)
        {
            case 1:
                BackgroundImage.sprite = Background1;
                break;
            case 2:
                BackgroundImage.sprite = Background2;
                break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# unity-game-engine

0
推荐指数
1
解决办法
159
查看次数

标签 统计

c# ×1

unity-game-engine ×1