Hi,
What I am trying to do is to have a starting sphere that the user has to touch. Once this is done, the starting sphere disappears and 3 spheres appear. The user has to touch them to make theme disappear.
After making the starting sphere disappear, the 3 spheres are instantiated, but when I touch them, they don’t get destroyed.
Here is my script :
public class ExerciceTest : MonoBehaviour
{
public GameObject startSphere;
public GameObject sphere;
public GameObject user;
private GameObject startSphereClone;
List<GameObject> sphereList;
private bool sphereSpawned;
// Start is called before the first frame update
void Start()
{
sphereSpawned = false;
startSphereClone = Instantiate(startSphere, new Vector3(0, 17, 8), Quaternion.identity);
}
// Update is called once per frame
void Update()
{
if (EcartV3(user.transform.position, startSphereClone.transform.position))
{
Destroy(startSphereClone);
SpawnSpheres(new Vector3(3, 3, 15), new Vector3(-2, 15, 15), new Vector3(6, 19, 18));
destroySpheresOnContact(sphereList);
}
}
private bool EcartV3(Vector3 a, Vector3 b)
{
return Vector3.SqrMagnitude(a-b) < 0.3f;
}
private bool SpawnSpheres(Vector3 sphereAPos, Vector3 sphereBPos, Vector3 sphereCPos)
{
if (!sphereSpawned)
{
GameObject sphereA = Instantiate(sphere, sphereAPos, Quaternion.identity);
GameObject sphereB = Instantiate(sphere, sphereBPos, Quaternion.identity);
GameObject sphereC = Instantiate(sphere, sphereCPos, Quaternion.identity);
sphereList.Add(sphereA);
sphereList.Add(sphereB);
sphereList.Add(sphereC);
sphereSpawned = true;
}
return sphereSpawned;
}
private void destroySpheresOnContact(List<GameObject>spheres)
{
foreach (GameObject sphere in spheres)
{
if (EcartV3(user.transform.position, sphere.transform.position))
{
Destroy(sphere);
}
}
}
}
It would be very helpful if any of you guys had any idea of what is wrong with my script,
Thank you