How can I have a code that randomly selects one word out of three repeat one its own without a button being pressed?
I want to write a code that will randomly choose one word out of three (apple,tiger,stick) and then execute the following code below. Then once the crazyappleprefab turns false, I would like the random word code to randomly select another word again and set off another instantiation and uninstantiation after a certain period of time. Like 5 seconds or something. Thanks!
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Appletime : MonoBehaviour //// This code codes for an object to instantiate every five seconds and have each object remain indefinitely. {
float TmStart; // this means time start
float TmLen = 5f; // this means length of time
public Text randomTextOutcome;
public Text Apple;
private int AppleCount;
public GameObject crazyApplePrefab;
// Start is called before the first frame update
IEnumerator Start()
{
AppleCount = 0;
yield return new WaitForSeconds(TmLen);
int x = Random.Range(-3, 3);
int z = Random.Range(-3, 3);
randomTextOutcome.text = "Apple".ToString();
Apple.text = AppleCount + 1.ToString();
Vector3 pos = new Vector3(x, 1, z);
GameObject crazyApple = Instantiate(crazyApplePrefab, pos, Quaternion.identity);
yield return new WaitForSeconds(10);
crazyApple.SetActive(false);
randomTextOutcome.text = "".ToString();
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613032407im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Random Number every 5 seconds 2 Answers
Help with Randomized Sprite on GUI button click 0 Answers
Randomly generate blocks on a flat map 0 Answers
Generate Random sequence of color sprites 0 Answers
Random values not different 2 Answers