Best practive when removing pickup gameObject and its instances
Hi Guys,
So in my game im using an infinite scrolling road using by scrolling the UVS instead of moving the road.
I have pickup's in at the moment and they are spawning at a random x position a"Pickup" gameObject.
My problem is that when the initial gameObject is destroyed on collision, how can i instantiate a game object that doesnt exist.
I know i could move the object to safety somewhere off screen but i feel thats a waste of resources and im sure there is a much more proficient way to do this.
So my question is how is this generally done ?
Here is my pickup class
// Update is called once per frame
void Update () {
PickUpMovement();
}
private void PickUpMovement()
{
z = Time.deltaTime * vertical;
transform.Translate(0, 0, -z);
}
private void OnTriggerEnter(Collider other)
{
Debug.Log("Entered");
Destroy(gameObject);
}
On collision with a pickUp delete the object.
Here is my spawn public GameObject[] objects; public float spawnTime = 6f; // How long between each spawn. private Vector3 spawnPosition;
void Start()
{
// Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
InvokeRepeating("Spawn", spawnTime, spawnTime);
}
public void Spawn()
{
spawnPosition.x = Random.Range(-7, 7);
spawnPosition.y = 0.5f;
spawnPosition.z = 65f;
Instantiate(objects[UnityEngine.Random.Range(0, objects.Length - 1)], spawnPosition, Quaternion.identity);
}
Your answer
Follow this Question
Related Questions
How to Make A Character Stop At Wall? 0 Answers
problem with tilemap collider and composite collider 1 Answer
Animator and collision issues 0 Answers
Adding different values to randomly created objects 0 Answers
[SOLVED] Problem with OnCollisionEnter2D script after upgrading to Unity 5.3(from Unity 4.6) 1 Answer