• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by sriteja25 · Jul 25, 2016 at 04:10 PM · c#unity 5transformtagcube

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
7

Answer by Jessespike · Jul 25, 2016 at 05:53 PM

There are several ways, which have been discussed on Unity Answers: (1), (2)

InvokeRepeating

 void Start () {
     InvokeRepeating("OutputTime", 1f, 1f);  //1s delay, repeat every 1s
 }

 void OutputTime() {
     Debug.Log(Time.time);
 }

Coroutine

 IEnumerator Start() {
     while(true) {
         yield return new WaitForSeconds(1f);
         OutputTime();
     }
 }

 void OutputTime() {
     Debug.Log(Time.time);
 }

Update

 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()"

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DerModster · Jun 21 at 09:32 AM 0
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to move buttons via Coroutine and transform.Translate 0 Answers

Problems With .rotate behavior 1 Answer

Flip 3D Character Rotation 180 on Y axis 1 Answer

Lerping Camera Between Two Points 0 Answers

how to make a cube change color by clicking a button? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges