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.