Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Point to stuff in script

Discussion in 'Scripting' started by hail_miki, Jul 19, 2018.

  1. hail_miki

    hail_miki

    Joined:
    Jul 17, 2018
    Posts:
    40
    Hi!

    so looking at my code , i get that these 2 lines.


    Code (CSharp):
    1.  public AudioMixer audioMixer;
    2. public Slider volSlider;
    gives me this blank spaces over here



    where i can drag the objects and mixers in.

    My question is , can i point them to that mixer and to my slider via straight from code?

    im trying to apply a theory that if i do that my program via singleton should recognize the object in the different scene since i didnt have to drag them in there in the first place and used the code directly to point them to it.

    kinda like a
    Code (CSharp):
    1. audiomixer.name = "MasterMixer";
    2.  
    3.  
    above doesnt work tho :((


    also would you know any other ways i could do this , as of right now ive resulted to adding an invisible sliders on some of my clips for it to recognize the volume changes i set in.
     
    Last edited: Jul 20, 2018
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You point to other objects and components through references. When you drag something to a field in the inspector, you're simply establishing a reference. There's other ways to establish references at runtime though. For example, you can assign a unique tag to an object, and a script on another object can find it with a call to GameObject.FindWithTag. You can then use GetComponent to get a reference to a specific script or other component attached to that GameObject. This can be used even to find GameObjects that are part of another scene that you have loaded additively.

    For example:
    Code (csharp):
    1. private void doSomethingCool()
    2. {
    3.     GameObject coolObject = GameObject.FindWithTag("Cool");
    4.     if (coolObject != null)
    5.     {
    6.         CoolScript coolScriptComponent = coolObject.GetComponent<CoolScript>();
    7.         if (coolScriptComponent != null)
    8.         {
    9.             coolScriptComponent.VeryCoolMethod();
    10.         }
    11.     }
    12. }
    So in the above you have an object that has been tagged with "Cool" with an attached MonoBehaviour script called CoolScript.cs, which has a public method called VeryCoolMethod. When calling doSomethingCool() it finds the first GameObject tagged with "Cool", then gets its component of class CoolScript, and then calls that public method.

    It is also a good idea to check at each step for null to avoid null reference exceptions if the object or component is not actually there, so I included that in the example. If you like to live dangerously, it could also be written as below.

    Code (csharp):
    1. private void doSomethingCool()
    2. {
    3.     GameObject.FindWithTag("Cool").GetComponent<CoolScript>().VeryCoolMethod();
    4. }
    There's a bunch of other "Find" style commands available that all work a bit differently. It is important to know that any of the Find calls, and GetComponent, are rather expensive performance wise. So it is recommended to call them infrequently and cache their results rather than calling them frequently in something like Update(). Also, GetComponent can be used to find a component of any type attached to a GameObject, not just instances of your own C# scripts. Yet another example:

    Code (csharp):
    1. Slider slider = GameObject.FindWithTag("AudioStuff").GetComponent<Slider>();
     
    Last edited: Jul 20, 2018
  3. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,057
    I'd avoid using GameObject.Find or FindWithTag. Find is bad for performance if you have a lot of objects in your scene as it has to iterate through them. FindWithTag & Find could also return a gameobject that you don't even want to have returned, if you have 2 objects named the same way or tagged the same way it might pick the first one in the editor and the other in a build.

    You could also use GetComponentInChildren<T> on a gameobject if the component is in one of the children.

    Personally I'd like to avoid Singletons, this guy gives a nice example why in his talk.