Hello, I’m trying to write a script that gives the user two choices. The user either chooses left or right and then it repeats. However, I’m trying to do the following:
- Destroy both objects when the user either presses left or right
- Make sure that both choices are not the same (e.g. left and right choices cannot both be healthy food)
So I’m trying to work on the first item and I’m having a lot of trouble. I’m REALLY new to scripting and C# in general so I’m not sure how to destroy the cloned objects instead of the prefab. I had a function for destroying the objects but it just deleted the prefabs instead. After that, I tried having a separate script for destroying objects that would be applied to cloned objects but I’m not sure how to apply them to cloned objects. I could really use some help here on the first part (and if you’re feeling generous the second part).
Here’s my code:
using UnityEngine;
using System.Collections;
public class randomFood : MonoBehaviour {
public float randFloat;
public GameObject healthyFood;
public GameObject unhealthyFood;
public GameObject notFood;
public Transform emptyObject;
void Start ()
{
StartGame ();
}
void Update ()
{
if (Input.GetKeyDown (KeyCode.LeftArrow)) {
print ("Left Choice");
StartGame ();
} else if (Input.GetKeyDown (KeyCode.RightArrow)) {
print ("Right Choice");
StartGame ();
}
}
void StartGame ()
{
randFloat = Random.Range (0, 70);
if (randFloat <= 40) {
Instantiate (healthyFood, emptyObject.position, emptyObject.rotation);
Debug.Log ("Healthy Food");
}
{
if (randFloat > 40 && randFloat <= 60) {
Instantiate (unhealthyFood, emptyObject.position, emptyObject.rotation);
Debug.Log ("Unhealthy Food");
}
{
if (randFloat > 60 && randFloat <= 70) {
Instantiate (notFood, emptyObject.position, emptyObject.rotation);
Debug.Log ("Not Food");
}
}
}
}
}
And here’s the destroy script:
using UnityEngine;
using System.Collections;
public class DestroyedFood : MonoBehaviour
{
void Start ()
{
if (Input.GetKeyDown (KeyCode.LeftArrow)) {
Destroy (gameObject);
} else if (Input.GetKeyDown (KeyCode.RightArrow)) {
Destroy (gameObject);
}
}
}
Please help me out! Thanks!