Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by hamin94 · Feb 07, 2018 at 07:27 AM · getcomponentclassgameobjectsbasics

GameObject Class understanding

Hello , just understanding the fundamentals of Unity I cant understand the GameObject variables in scripts. where are they actually ? are they in hierarchy when we declare them ?

look at this example :

 GameObject testObject;
 Light testLight;
 
 Void Start() {
 testLight = GetComponent<Light>();
 //or
 testLight = testObject.GetComponent<Light>();

What is different when use the testObject and when we dont use it? what does it actually do ?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Cherno · Feb 07, 2018 at 07:38 AM

The following line testLight = GetComponent<Light>(); calls Component.GetComponent(). As indicated in the documentation, this function returns the Light component from the GameObject this script is attached to.


The following line testLight = testObject.GetComponent<Light>(); calls GameObject.GetComponent(). As indicated in the documentation, it returns the Light component from the GameObject referenced by the testObject variable.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hamin94 · Feb 07, 2018 at 08:14 AM 0
Share

thanks for the reply but what is the entity of testObject ? I can understand gameobjects inhierarchy and scene but what are gameobjects in scripts ? so when i have a testObject with a lightcomponent , where the light will be visible ? or it just stores the value of the light ?

i dont know my question is clear or not but im stucked understanding in creating a gameobject inside of a script of a real gameobject in the scene.

avatar image Hellium hamin94 · Feb 07, 2018 at 08:28 AM 1
Share

Let me take an example :

Supposing you have created a script called LightSwitch. This script is attached to a gameobject called LightSwitch in your scene. Your player will be able to interact with the LightSwitch gameobject. He can switch it on and switch it off.


The LightSwitch script will be responsible for turning on and off a Light bulb. To do so, you can reference the LightBulb gameobject in your LightSwitch script. This script will control whether the Light component attached to the LightBulb gameobject is on or off.

avatar image hamin94 Hellium · Feb 07, 2018 at 09:37 AM 0
Share

$$anonymous$$any Thanks Hellium

avatar image meat5000 ♦ hamin94 · Feb 07, 2018 at 09:39 AM 1
Share

A GameObject in a script is simply a Variable in the shape of a GameObject. Exactly the same as an int or string or anything, but GameObject shaped. So it'll hold information that looks like a GameObject. Because GameObject is a class it comes with a whole bunch of stuff programmed in to it and you can access all that stuff through your variable. You can either fill it with stuff that looks like a GameObject yourself, like a paper mockup of sorts, or you can make it point with a Reference to an actual GameObject and then you can play around with that Instance. Your Light testLight is at first simply a variable in the Shape of a Light (which has been created separately). At first its just a 'mockup', just a placeholder, but with GetComponent you can fill that variable with a Reference to an actual Light.

avatar image hamin94 meat5000 ♦ · Feb 07, 2018 at 09:48 AM 0
Share

Clearest comment , $$anonymous$$any Thanks

avatar image hamin94 · Feb 07, 2018 at 08:27 AM 0
Share

So we use gameobject variables in scripts to reference physical gameobject in the scene or other scenes ans scripts ?

avatar image Hellium hamin94 · Feb 07, 2018 at 08:51 AM 1
Share

You can reference a gameobject in the scene, if you want to interact with it or a prefab in your Project view if you want to clone (instantiate) it.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

79 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Class for spawning units (gameobjects) 1 Answer

Accessing a method / void from another class? 0 Answers

Is it possible to set variables for classes? 2 Answers

How do I find an external component from inside a class? 0 Answers

How do I instantiate a class Object, then store it in an Array of Class Objects in C#? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges