I have this code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Ejercicio6 : MonoBehaviour
{
public GameObject prefab;
public float velocity;
public Vector3 direction;
public int bounceCount;
bool IsOutOfScreenX()
{
Vector3 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
return screenPosition.x < 0 || screenPosition.x > Screen.width;
}
bool IsOutOfScreenY()
{
Vector3 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
return screenPosition.y < 0 || screenPosition.y > Screen.height;
}
// Start is called before the first frame update
void Start()
{
velocity = Random.Range(7f, 7f);
direction = new Vector3(Random.Range(-1f, 1f), Random.Range(-1f, 1f), 0f);
}
// Update is called once per frame
void Update()
{
transform.Translate(direction * velocity * Time.deltaTime);
if (IsOutOfScreenX())
{
direction.x *= -1f;
bounceCount++;
if (bounceCount >= 10)
{
Destroy(gameObject);
GameObject newCube = Instantiate(prefab, new Vector3(0f, 0f, 0f), Quaternion.identity);
}
}
if (IsOutOfScreenY())
{
direction.y *= -1f;
bounceCount++;
if (bounceCount >= 10)
{
Destroy(gameObject);
GameObject newCube = Instantiate(prefab, new Vector3(0f, 0f, 0f), Quaternion.identity);
}
}
}
}
I need to make a gameObject to bounce 10 times into the borders from the screen and then to destroy it and create a new object that repeats the same process.
I could manage to make the first object to bounce the 10 times, destroy it and create a new object that makes the same process, but when the third object is created it’s name says Cube(Clone)(Clone).
I think is because I’m using a prefab which has this script, but it is the only way I could make the objects to move.
How can I make the object to do the process, then being destroyed, and create a new one which makes the same process, and repeat this till I stop the program.