Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.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# Unity c游戏对象的覆盖_C#_Unity3d - Fatal编程技术网

C# Unity c游戏对象的覆盖

C# Unity c游戏对象的覆盖,c#,unity3d,C#,Unity3d,a和objeler[]是游戏对象。我使用了5次YokeTime。我得到一些带有a的随机对象,并赋予它们重力。但只有一个对象在进入bekleme后失去重力。最后一个a是在9秒前覆盖到所有使用YokeTime的a对象的。我如何让bekleme获得所有游戏对象,而不是最后一个 void YokEtme(){ int x = Random.Range (35, 0); a = objeler [x]; z = a.GetComponent<Trans

a和objeler[]是游戏对象。我使用了5次YokeTime。我得到一些带有a的随机对象,并赋予它们重力。但只有一个对象在进入bekleme后失去重力。最后一个a是在9秒前覆盖到所有使用YokeTime的a对象的。我如何让bekleme获得所有游戏对象,而不是最后一个

    void YokEtme(){

    int x = Random.Range (35, 0);       
    a = objeler [x];
    z = a.GetComponent<Transform> ().position;
    a.GetComponent<Rigidbody> ().useGravity = true;
    Debug.Log (a);


    StartCoroutine (bekleme ());
}

    IEnumerator  bekleme (){

    yield return new WaitForSeconds (9);
        a.GetComponent<Rigidbody> ().useGravity = false;
        a.GetComponent<Rigidbody> ().constraints = RigidbodyConstraints.FreezePosition;
        a.transform.position= z;
    a.GetComponent<Rigidbody> ().constraints = RigidbodyConstraints.None;
    Debug.Log (a);



    }

由于问题中缺少一些上下文,请允许我大胆假设:

提供的代码是MonoBehavior的一部分。 a和z是monobhavior的字段/属性。 你要做的是从objeler中随机挑选五个游戏对象,对它们施加重力,几秒钟后取消施加重力。 如果我的假设是正确的,问题很简单。只有一个选定对象失去重力的原因是,Unity一次只允许执行一个协程。 也就是说,尽管您调用了YokeTime五次,每次调用都会导致StartRoutineBeklee,并且预期会有五个并行运行的协同路由,但实际上,每次调用StartRoutineBeklee都会丢弃以前运行的协同路由,并启动一个新的协同路由

因此,一个可能的解决方案是在一个协同程序实例中处理所有五个游戏对象。例如:

using System.Collections.Generic;
using UnityEngine;
public class SomeMono : MonoBehaviour
{
    private List<GameObject> _controlledGOs = new List<GameObject>();
    private List<float> _loggedZs = new List<float>();

    void YokEtme()
    {
        int x = Random.Range(35, 0);       
        a = objeler [x];

        a.GetComponent<Rigidbody>().useGravity = true;

        _controlledGOs.Add(a);
        _loggedZs.Add(a.GetComponent<Transform>().position);
    }

    void SetGravityForGameObjects()
    {
        for (var i = 0; i < 5; i++)
            YokEtme();

        StartCoroutine()
    }

    IEnumerator bekleme ()
    {
        yield return new WaitForSeconds (9);
        for (var i = 0; i < _controlledGOs.Count; i++)
        {
           var a = _controlledGOs[a];
           a.GetComponent<Rigidbody>().useGravity = false;
           a.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePosition;
           a.transform.position= _loggedZs[i];
           a.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
           Debug.Log (a);
        }
    }
}

因为我是新手,学习起来很难,但最终还是成功了!谢谢,帮了大忙。