Search Unity

Feedback Scene should be contains global components

Discussion in 'Editor & General Support' started by kl3in3rhack3r, Mar 5, 2019.

  1. kl3in3rhack3r

    kl3in3rhack3r

    Joined:
    Nov 22, 2018
    Posts:
    15
    Hi @all,

    sometimes I need in a scene global components they must exists for this scene. To use it I must currently create an empty game object in the scene graph. If I search for something global components I must search it in the big scene tree. That is very confusing and make it harder to service the game / software or to deliver it to some other developer.
    Instead the global scene components should be added directly to the scene. In code you access it so:

    gameObject.scene.GetComponent<MyComponent>();

    So I can search for the components under the scene root node. This make it easier to use this.

    What you think about this?
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    If it is working for you, sounds good to me! But honestly I didn't understand your question. Would GameObject.Find work for you also?
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I don't exactly understand your question, but using singletons or static class properties/methods are common ways of doing what I think you mean.
     
  4. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    OP is basically asking for a method like
    Object.FindObjectsOfType<T>()
    , but a method that only searches a specific scene.

    This would be a convenient feature, but it's also not difficult to do yourself:
    Code (CSharp):
    1. GameObject[] objArray = scene.GetRootGameObjects();
    2. List<T> foundList = new List<T>();
    3. foreach (GameObject currentObject in objArray)
    4. {
    5.     T[] foundInChildren = currentObject.GetComponentsInChildren<T>();
    6.     foundList.AddRange(foundInChildren);
    7. }
    8.  
    9. // foundList is now a list of all instances of component "T" in the scene