Inclusion and deactivation of the object in code C# Unity3d



  • using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    
    public class StarHolder : MonoBehaviour {
    
    public List<GameObject> ListStars = new List<GameObject>();
    public GameObject Father;
    public bool onoff = true;
    public float TimeDela;
    
    void Start()
    {
        StartCoroutine(GeneratingStar()); //Запуск коротина
    }
    
    void Update () {
    
    }
    public IEnumerator GeneratingStar(){
        TimeDela = Random.Range (3f, 10f);
    
        foreach (Transform item in Father.transform) {
            ListStars.Add (item.gameObject);
            if (!item.gameObject.activeSelf) {
                item.gameObject.SetActive (true);            //Для линейного включения
                yield return new WaitForSeconds(TimeDela); 
            }
        }
        //  foreach(GameObject item in ListStars){
        //  ListStars [Random.Range (0, ListStars.Count)].SetActive (true); //Для рандомного включения
        //  yield return new WaitForSeconds(TimeDela); 
        //}
      }
     }
    


  • You can create GameObjectto add stars as a child, and to keep them for convenience. List<ActiveStar>Let's say StarHolder:

    public class StarHolder: MonoBehaviour {
    
    List&lt;ActiveStar&gt; m_Stars = new List&lt;ActiveStar&gt;();
    
    
    void Update () {
        // прогоняем звёзды
        // если звезда активна, то выключаем, иначе включаем
        Stars.ForEach(star =&gt; star.SetActive(!star.activeSelf));
    }
    

    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2