- Home /
Enable GameObjects one by one each after 1 sec.
Hi, I am trying to make a kids education game, where i want to show how to count objects. Basic idea is to make few scenes and have few objects in each scene. Then I want numbers to appear on top of those objects 1 by 1 after 1 sec each, as if I am counting them and showing it to the user. I have done it and somehow its working too, but i think there is surely a better way to do it. Please advice.
Here is my part of code, which i came up with.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public GameObject count1, count2, count3, count4, count5;
public void ShowTheCounting()
{
Invoke("Count1", 1f);
Invoke("Count2", 2f);
Invoke("Count3", 3f);
Invoke("Count4", 4f);
Invoke("Count5", 5f);
}
public void Count1()
{
count1.SetActive(true);
AudioSource.PlayClipAtPoint(sound1, transform.position);
}
public void Count2()
{
count2.SetActive(true);
AudioSource.PlayClipAtPoint(sound1, transform.position);
}
public void Count3()
{
count3.SetActive(true);
AudioSource.PlayClipAtPoint(sound1, transform.position);
}
public void Count4()
{
count4.SetActive(true);
AudioSource.PlayClipAtPoint(sound1, transform.position);
}
public void Count5()
{
count5.SetActive(true);
AudioSource.PlayClipAtPoint(sound1, transform.position);
}
}
I want to create just one script and handle all scenes. So if scene 1 has 3 objects, It should only count, 123, if scene 2 has 5 objects, it should count till 5 and so on.
Kindly help me.
TIA
Answer by Chik3r · Feb 06, 2019 at 09:15 PM
Using a Coroutine:
public GameObject[] counts;
public float timeBetween = 1f; // Time in seconds
public void ShowTheCounting() { // You call this function
StartCoroutine(Count());
}
public IEnumerator Count()
{
foreach (GameObject count in counts)
{
count.SetActive(true);
yield return new WaitForSeconds(timeBetween); // Waits for the time set in timeBetween, affected by timeScale.
}
}
You have to assign all your counts to counts
. Here's an example video:
Update:
To add a sound to each count, you can create a class to store the sounds and objects. It would look like this:
public CountItem[] counts;
public float timeBetween = 1f; // Time in seconds
...
public IEnumerator Count()
{
foreach (CountItem count in counts)
{
count.item.SetActive(true);
count.sound.Play(0); // Play the sound
yield return new WaitForSeconds(timeBetween); // Waits for the time set in timeBetween, affected by timeScale.
}
}
class CountItem
{
public GameObject item;
public AudioSource sound;
}
And then in the inspector, you assign the count to item
and your sound to sound
.
Thank You so much. It worked like a charm. You are a star :D
@chiker - One quick question- what if i have to add sound (vocals of 1-2-3-4-5.....) on those numbers. Like when number "1" appears- the sound would be "one", same for 2, 3, 4...and so on.
Answer by Cornelis-de-Jager · Feb 06, 2019 at 09:13 PM
You can use a combination of Co routines and IEnemerables to get what you want. Below I use the Stack : IEnemerable to give me an easy way to get and remove elements from the top of the collection.
using System.Collections.Generic;
public Stack<GameObject> Spawnables;
public int waitTime;
public void StartCountingProcess () {
StartCoroutine (CountingCoroutine());
}
Ienumerator CountingCoroutine () {
while (Spawnables.Count() > 0){
yield return new WaitForSeconds (waitTime);
var spawnable = Spawnables.Pop();
Instantiate (spawnable, Vector3.Zero /* Position Goes here*/, Quaternion.Identity /* Rotation goes here*/);
}
}
Answer by toddisarockstar · Feb 06, 2019 at 10:18 PM
you can make this much simpler with arrays.
add this to the top of your script:
using System.Collections.Generic;
this should work:
//drop your objects representing numbers into the inspector
public GameObject[] counts = new GameObject[5];
public int index;
public int[] cur;
public void StartScene (params int[] numbers) {
index = 0;cur = numbers;
foreach(GameObject g in counts){g.SetActive(false); }
StartCoroutine (CountingCoroutine());
}
IEnumerator CountingCoroutine () {
while (index < cur.Length){
yield return new WaitForSeconds (2);
counts[cur[index]].SetActive(true);
index++;}
}
void Start(){
// these object numbers will light up in this order
StartScene (1, 2, 3);
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Enable and disabled parent contraint with c# 1 Answer
Disabling a script in c# not working for some reason 1 Answer
Switching Weapons (C#) 2 Answers