Friday, September 9, 2022
HomeGame Developmentunity - How do I reset the sport object motion after I...

unity – How do I reset the sport object motion after I setActive the thing to false? (ObjectPool downside)


That is all from objectpooling. As an alternative of deleting it, it mentioned to set the gameobject to false. The issue is, it did not reset when it got here again SetActive(true). What ought to I do?

that is the script of that object

    public class ObjectMovement : MonoBehaviour
{
    [SerializeField] float velocity = 5f;
    [SerializeField] int PositionIndex;
    float TimeLeft;

    non-public void OnEnable()
    {
        TimeLeft = Random.Vary(2, 6);
    }
    non-public void Replace()
    {
        MovingIn();
    }

    void MovingIn()
    {
        TimeLeft -= Time.deltaTime;
        change (PositionIndex)
        {
            case 1:
                rework.Translate(Vector2.proper * velocity * Time.deltaTime);
                if (TimeLeft <= 0)
                {
                    TimeLeft = Random.Vary(1, 3);
                    PositionIndex = 2;
                }
                break;

            case 2:
                rework.Translate(Vector2.down * velocity * Time.deltaTime);
                if (TimeLeft <= 0)
                {
                    TimeLeft = Random.Vary(0.2f, 1);
                    PositionIndex = Random.Vary(3, 5);
                }
                break;

            case 3:
                rework.Translate(Vector2.proper * velocity * Time.deltaTime);
                break;
            case 4:
                rework.Translate(Vector2.left * velocity * Time.deltaTime);
                break;

        }
    }

    non-public void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("wall"))
        {
            gameObject.SetActive(false);
        }
    }

}

and that is the script from objectpool

public class ObjectPooler : MonoBehaviour
{
    [SerializeField] GameObject prefab;
    [SerializeField] int poolsize = 10;

    Checklist<GameObject> _pool;

    non-public void Awake()
    {
        _pool = new Checklist<GameObject>();
        CreatePooler();
    }

    void CreatePooler()  //Add in to "CreateInstance" to retailer the prefab
    {
        for (int i = 0; i < poolsize; i++)
        {
            _pool.Add(CreateInstance());
        }
    }

    GameObject CreateInstance()   //Retailer it. Solely to be activated (setActive, true) when wanted
    {
        GameObject newInstance = Instantiate(prefab);
        newInstance.SetActive(false);
        return newInstance;
    }

    public GameObject GetInstanceFromPool()  
    {
        for (int i = 0; i < _pool.Rely; i++)  //if prefab not getting used (setActive / lively within the hierachy), returning inactive object
        {
            if (!_pool[i].activeInHierarchy)
            {
                return _pool[i];
            }
        }

        return CreateInstance(); //So if getting used, will retailer in CreateInstance
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments