Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从范围属性中获取最小值和当前值之间的随机数?_C#_Unity3d - Fatal编程技术网

C# 如何从范围属性中获取最小值和当前值之间的随机数?

C# 如何从范围属性中获取最小值和当前值之间的随机数?,c#,unity3d,C#,Unity3d,最小值为20,最大值为300。 例如,如果在inspector中,滑块的当前值为120,则选择一个介于20和120之间的随机数,如果在运行游戏之前或游戏运行时,我将该值更改为77,则下一个随机数应介于20和77之间 我希望NPC每做一件事,每X个随机秒。 例如,在运行游戏后,如果随机数是20或90,那么在20秒或90秒后,NPC将做一些事情。然后是下一个随机数,如果是199,那么在199秒后NPC会做同样的事情(某事),依此类推 using System.Collections; using S

最小值为20,最大值为300。 例如,如果在inspector中,滑块的当前值为120,则选择一个介于20和120之间的随机数,如果在运行游戏之前或游戏运行时,我将该值更改为77,则下一个随机数应介于20和77之间

我希望NPC每做一件事,每X个随机秒。 例如,在运行游戏后,如果随机数是20或90,那么在20秒或90秒后,NPC将做一些事情。然后是下一个随机数,如果是199,那么在199秒后NPC会做同样的事情(某事),依此类推

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class RandomTester : MonoBehaviour
{
    public List<GameObject> npcs;
    [Range(20, 300)]
    public int timeVisit = 20;
    public string nextVisitTime;

    // Start is called before the first frame update
    void Start()
    {
        npcs = GameObject.FindGameObjectsWithTag("Npc").ToList();


    }

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

    }
}
使用系统集合;
使用System.Collections.Generic;
使用System.Linq;
使用UnityEngine;
公共类随机测试仪:单行为
{
公开名单NPC;
[射程(20300)]
公共int时间访问=20;
公共字符串下一个可视时间;
//在第一帧更新之前调用Start
void Start()
{
npcs=GameObject.FindGameObjectsWithTag(“Npc”).ToList();
}
//每帧调用一次更新
无效更新()
{
}
}

生成20到300之间的X个随机数并将其添加到列表中:

Random r = new Random();
List<int> rands = new List<int>();
for (int i = 0; i < X; i++)
{
    rands.Add(r.Next(20, 300));
}
Random r=new Random();
List rands=新列表();
对于(int i=0;i
也许我解释得不好

这就是我想要的,并且正在发挥作用。我从范围属性滑块的最小值和当前值之间取一个随机数,即inspector中的滑块

然后,我将在每次我更改inspector中的值时,它拾取一个随机数,它将启动一个协同程序,等待随机拾取的秒数,当它完成时,NPC将执行一些操作

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class RandomTester : MonoBehaviour
{
    public List<GameObject> npcs;
    [Range(20, 300)]
    public int timeVisit = 20;
    public string nextVisitTime;

    private int oldtimevisit;

    // Start is called before the first frame update
    void Start()
    {
        npcs = GameObject.FindGameObjectsWithTag("Npc").ToList();

        oldtimevisit = timeVisit;
    }

    // Update is called once per frame
    void Update()
    {
        if(timeVisit != oldtimevisit)
        {
            StopAllCoroutines();
            Debug.Log(Random.Range(20,timeVisit));

            var timetowait = Random.Range(20, timeVisit);
            StartCoroutine(Waiting(timetowait));
        }

        oldtimevisit = timeVisit;
    }

    IEnumerator Waiting(int waitingtime)
    {
        yield return new WaitForSeconds(waitingtime);

        DoSomethingWithNpcs();
    }

    private void DoSomethingWithNpcs()
    {

    }
}
使用系统集合;
使用System.Collections.Generic;
使用System.Linq;
使用UnityEngine;
公共类随机测试仪:单行为
{
公开名单NPC;
[射程(20300)]
公共int时间访问=20;
公共字符串下一个可视时间;
私人访问;
//在第一帧更新之前调用Start
void Start()
{
npcs=GameObject.FindGameObjectsWithTag(“Npc”).ToList();
oldtimevisit=timeVisit;
}
//每帧调用一次更新
无效更新()
{
if(timeVisit!=oldtimevisit)
{
StopAllCoroutines();
Debug.Log(Random.Range(20,timeVisit));
var timetowait=随机范围(20,时间访问);
开始例行程序(等待(timetowait));
}
oldtimevisit=timeVisit;
}
IEnumerator等待(int waitingtime)
{
返回新的WaitForSeconds(waitingtime);
DoSomethingWithNpcs();
}
私有void DoSomethingWithNpcs()
{
}
}