Search Unity

Maybe a stupid question

Discussion in 'Scripting' started by EdGunther, Jun 20, 2019.

  1. EdGunther

    EdGunther

    Joined:
    Jun 25, 2018
    Posts:
    183
    Script B (attached to many objects) references Script A (just one exists, attached to Object A). Object A is not often active in hierarchy, so when Script B tries to Find Object A with tag in the Awake() function, it often fails due to Object A's active status.

    I thought about making a function like
    Code (CSharp):
    1.     void FindObjectA()
    2.     {
    3.         objectA = GameObject.FindGameObjectWithTag("A");
    4.     }
    that is called every time I need to reference it
    Code (CSharp):
    1.         if (objectA != null)
    2.             FindObjectA();
    But that seems like a REALLY BAD solution.

    Assigning the gameobject in the inspector is not an option.
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Lookup the singleton pattern
     
  3. EdGunther

    EdGunther

    Joined:
    Jun 25, 2018
    Posts:
    183
    I haven't used a singleton in a long time. I heard it was bad practice.
     
  4. Thibault-Potier

    Thibault-Potier

    Joined:
    Apr 10, 2015
    Posts:
    206
    No it's Not. Finding gameobject by tag is
     
    Vryken, Antypodish and SparrowGS like this.
  5. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    It is not a bad practice to use singletons. It is just some people abusing it.
     
    SparrowGS likes this.
  6. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Yeah, just do it in a sane manner, you can use everything in a wrong way.

    I never had a real probkem with them.
     
  7. EdGunther

    EdGunther

    Joined:
    Jun 25, 2018
    Posts:
    183
    Find Object by tag is bad practice? What about transform.Find() for children? What should I do instead when I need to find Object(s) in my scene Hierarchy?
     
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,770
    Just cache objects into some collection.
     
  9. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Its fine if its one call every now and then, but I personally only use them on load screens.
     
  10. EdGunther

    EdGunther

    Joined:
    Jun 25, 2018
    Posts:
    183
    Right now I'm declaring variables and only using those Find functions on Awake at the start of the scene, then referencing the variable to execute the code

    For example:
    Code (CSharp):
    1.    
    2. NewTimerScript myTimer;
    3.  
    4. void Awake()
    5. {
    6.         myTimer = transform.Find("Timer Holder").GetComponent<NewTimerScript>();
    7. }
    8.  
    9. void OtherFunctionsInThisScriptThatReferenceTheTimer()
    10. {
    11.        myTimer.ThanksForYourHelpGuys(iReally, appreciate, it);
    12. }
    13.  
    Is this a bad way of going about this?
     
  11. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Then its fine, they are a problem when calling them during gameplay
     
    Antypodish and EdGunther like this.