How to call a function every second ?
I am trying to implement a virtual grocery store. I want to show the number of products selected of each item.
My basic idea of implementation is i set a tag to all the elements of a parent and transform them to a new parent object ( Empty in the beginning) whenever an object in the inventory is clicked.
Now i have a code attached to cubes which will transform the object from one parent to another. I have another script attached to the empty parent object (It will contain child when an object in the inventory is clicked) which checks the number of children component using "Tag" and returns the number of components
The script runs as soon as the play button is clicked, But i want the script attached to the empty object to run every second so that it can return the count of the gameObject as soon as a child is attached.
Here's the code for my products (Cubes here) using UnityEngine; using System.Collections;
public class cubeTag : MonoBehaviour {
public GameObject cube1;
public GameObject cube2;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnMouseDown()
{
cube1.transform.parent = cube2.transform;
//transform.gameObject.tag = "cube";
}
}
Here's the code for my Empty parent gameObject using UnityEngine; using System.Collections;
public class CubeContainer : MonoBehaviour {
int c=0;
public GameObject Children;
// Use this for initialization
void start( )
{
foreach (Transform child in transform) {
if (child.tag == "cube") {
c++;
}
}
Debug.Log (c);
}
}
I tried using void Update() function it dint work. I also tried using 2 different functions and call the function which checks for children every second through other method, it dint work either. It only prints the value of c when I define it in void start() method. Please help me.
Answer by Jessespike · Jul 25, 2016 at 05:53 PM
There are several ways, which have been discussed on Unity Answers: (1), (2)
void Start () {
InvokeRepeating("OutputTime", 1f, 1f); //1s delay, repeat every 1s
}
void OutputTime() {
Debug.Log(Time.time);
}
IEnumerator Start() {
while(true) {
yield return new WaitForSeconds(1f);
OutputTime();
}
}
void OutputTime() {
Debug.Log(Time.time);
}
float elapsed = 0f;
void Update() {
elapsed += Time.deltaTime;
if (elapsed >= 1f) {
elapsed = elapsed % 1f;
OutputTime();
}
}
void OutputTime() {
Debug.Log(Time.time);
}
Also the start function in CubeContainer needs to be upper-cased, Use "Start()"
@Jessespike Where do you enter the function that you want repeated every second? I am trying to perform
transform.Translate(Vector3.left speed Time.deltaTime);
for a specific amount of seconds, and then run transform.position = new Vector3(445, 455, 436);
after that amount of seconds. How would I do that?
Your answer
Follow this Question
Related Questions
Trying to move buttons via Coroutine and transform.Translate 0 Answers
Problems With .rotate behavior 1 Answer
Skip OnTriggerEnter isn't working at all 1 Answer
Flip 3D Character Rotation 180 on Y axis 1 Answer
List update 0 Answers