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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Connecting Unity Components to Script

Discussion in 'Scripting' started by patmaric, Feb 1, 2020.

  1. patmaric

    patmaric

    Joined:
    Dec 5, 2019
    Posts:
    15
    I watched a tutorial where an empty GameObject was created, and from there all “connections” were made to the script. I see other tutorials where the scripts are split so each one can be independently connected directly to the corresponding component. Are both methods just as good? I like creating the empty Game Object as I find it more organized, but are there times when connecting directly to the component sometimes necessary? I am having issues with Unity crashing and think this may be my problem.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    I don't really know what you are asking to be honest. Adding components to an empty GameObject is no different then selecting say a 3d shape from the drop down menu which essentially is the same as creating an empty GameObject and then adding the components needed to make it a 3D model for you.

    What do you mean by "connecting". Can you give a better explanation of what that means for you?
     
  3. diXime

    diXime

    Joined:
    Oct 2, 2018
    Posts:
    162
    Hello hello,
    I really don't get you here. When you make an empty GameObject, it is connected to quite nothing at all, maybe to the Transform script.
    To "connect", if I understand you well, you can either make a script reference (meaning you keep track of your GameObject in your code, either because you created there or there is a relative path (parent, child, tag maybe?), either plug it into Unity by setting a public variable to said script and drop it into your GameObject.

    For exemple, I'm currently working on inverse kinematics, and for easy testing I'm plugging the target that way:


    in my Arm script, this line just looks like:
    Code (CSharp):
    1. public GameObject target;
    I made it that way so I just need to drag and drop the GameObject into the Target field.
     
  4. patmaric

    patmaric

    Joined:
    Dec 5, 2019
    Posts:
    15
    Yeah, I think I need to get more experience before I start asking questions. I see now that the first tutorial simply created an empty Game Object, then dragged the script onto that, then dragged that onto the components.
     
  5. diXime

    diXime

    Joined:
    Oct 2, 2018
    Posts:
    162
    Don't hesitate to ask questions if you feel lost. There are no stupid questions - only stupid answers.
     
  6. patmaric

    patmaric

    Joined:
    Dec 5, 2019
    Posts:
    15
    Thanks!