user7227160
user7227160

Reputation:

Unity c# gameobject's override

a and objeler [] are gameobjects.I use yoketme 5 times.I get some random objects with a and give them gravity.But only 1 of the objects lose gravity after they get in bekleme.The last a is the one overwriten to all a objects that uses yoketme before timer(9 seconds).How can i make bekleme to take all gameobjects and not the last one only?

    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);



    }

Upvotes: 0

Views: 318

Answers (1)

YAC
YAC

Reputation: 435

Since some contexts are missing from the question, please allow me to boldly assume that:

  1. The provided code are part of a MonoBehaviour.
  2. a and z are fields/properties of the MonoBehaviour.
  3. What you want to do is to randomly pick five gameobjects from objeler, apply gravity to them, and unapply gravity after a few seconds.

If my assumptions are correct, the problem is pretty simple. The reason that only one chosen object loses gravity is because that Unity allows only one coroutine being executed at a time.
That is, even though you called YokEtme five times and each will lead to StartCoroutine(bekleme()) and expected that there will be five coroutine running in parallel, actually each call to StartCoroutine(bekleme()) will discard the previously running coroutine and start a new one.

So a possible solution is to handle all the five gameobjects in one coroutine instance. Such as:

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);
        }
    }
}

Upvotes: 1

Related Questions