Calling function every x seconds but objects disappearing instantly
function is being called properly however the objects only flash into view then disappear. There is no external source causing - it works well if a keystroke calls the function within update, but on any sort of time interval, whether Invoke() or otherwise - the objects only flash for an instant
var timeBetweenTris:float = 0;
function Start () {
}
function Update () {
timeBetweenTris += Time.deltaTime;
if(timeBetweenTris >= 5) {
GenerateTri();
timeBetweenTris = 0;
}
}
function GenerateShape() {
var randomNumber = Random.Range(1,9);
if(randomNumber < 4) {
return sphere;
}
else if(randomNumber < 7) {
return capsule;
}
else {
return cube;
}
}
function GenerateTri() {
Instantiate(GenerateShape(), positionTop, Quaternion.identity);
Instantiate(GenerateShape(), positionLeft, Quaternion.identity);
Instantiate(GenerateShape(), positionRight, Quaternion.identity);
}
Answer by Ali-hatem · Mar 16, 2016 at 11:18 AM
as i see you are trying to wait for some time before calling
GenerateTri();
but it will work for the first time becauseTime.deltaTime
start from 0.02 & will keep increasing so whenTime.deltaTime
geting more than 5 thentimeBetweenTris += Time.deltaTime;
wil bee 6+0 or 7+0 sotimeBetweenTris
will bee always >5 .else if(randomNumber >3 && < 7)
lastly if i understand you want to instantiate a 3 random objects after a delay of time you can use much easier way :
public GameObject[] objs;//attach objects to this array in inspector. void Start () { StartCoroutine (GenerateTri()); } IEnumerator GenerateTri() { Instantiate(objs[Random.Range (0, objs.GetLength (0))], positionTop, Quaternion.identity); Instantiate(objs[Random.Range (0, objs.GetLength (0))], positionLeft, Quaternion.identity); Instantiate(objs[Random.Range (0, objs.GetLength (0))], positionRight, Quaternion.identity); yield return new WaitForSeconds (5f); }
EDIT:
Hi,
thank you so much for your answer. It turned out that the specific issue was related to a Destroy script which was not active - however was unaware that inactive scripts can still influence an object.
Your implementation amounts to one instantiation, however repeating generation was required.
Using an Array is much better thank you.
Your answer
Follow this Question
Related Questions
Calling a function from another script? Not WORKING?! 2 Answers
How do you access the Scripts, Functions, and Bools within an array of GameObjects? 0 Answers
A method running each millisecond in real time 0 Answers
Cannot convert 'UnityEngine.GameObject' to 'UnityEngine.Vector3' Why is this? 1 Answer