Home Game Development unity – How do I reset the sport object motion after I setActive the item to false? (ObjectPool downside)

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

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

[ad_1]

This is all from objectpooling. Instead of deleting it, it mentioned to set the gameobject to false. The downside 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 pace = 5f;
    [SerializeField] int PositionIndex;
    float TimeLeft;

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

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

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

            case 3:
                rework.Translate(Vector2.proper * pace * Time.deltaTime);
                break;
            case 4:
                rework.Translate(Vector2.left * pace * 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;

    List<GameObject> _pool;

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

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

    GameObject CreateInstance()   //Store it. Only 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.Count; 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
    }
}

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here