Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
4
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
22

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, 2019 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.

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

Skip OnTriggerEnter isn't working at all 1 Answer

Flip 3D Character Rotation 180 on Y axis 1 Answer

List update 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges