In Unity everything we work on is a GameObject, either it is a cube, a car, a plane, UI or Particle effects. Each GameObject have its own component. The basic component in all GameObject is his transform.



Transform component is deals with position rotation and scaling of that GameObject. Components will be discussed in later post today we are going to learn about GameObject only.

Now as we know all the things in our active scene is a GameObject, but as a programmer how can we access those GameObject and their components using our script.
Note: Scripts are also Components of our GameObject.
Actually this is pretty easy and can be done in many ways.


First Way:  Drag and Drop the GameObject into script in inspector window.

Step1:Create a script name it as you feel like and attach it to an empty GameObject.
To create an empty GameObject right click in hierarchy window and click on empty GameObject.

Step2:Write the following code.


You will notice in unity in your empty GameObject script a small space is appeared with a name Player and box written GameObject in it.


That GameObject means the type of item that box can take. Try changing the type in script and you will notice that text written in that box also changed.
If you don’t type the public keyword the small Dialog box will not appear.

Step3:Drag and drop the GameObject in that box you want to use.

Second way:Finding GameObject with its name

Step1:Write the following Code in your previous script and save it.


Step2: Play the game in unity, your script will automatically find the GameObject if the name typed in “” (in script) matches the name of GameObject in unity.


Third Way:Finding GameObject with Tag. This way is useful when finding one or more game objects.

Finding Single GameObject

Step1:Write the following Code in your script.


Step2:Go to unity and select the GameObject you want to find and below its name find a dialog box with text written untagged click on it and then click on add tag.



A new dialog box will appeared in inspector named Tags and Layers.


In tags field click on + button and write the tag name as specified in script.
Then click on save.
Note you have just created the tag but not attached it to your GameObject.

Step3:Now select your GameObject and click on untagged and find the tag made by you and just click on it.
Now run the game and script will find your object.


Multiple GameObject
To find Multiple GameObject we have to tag all the game objects with same tag.
Step1: Write the code as shown below:



You have to create an array of type GameObject[] so that you can store multiple objects of same data type. Remember why array is used.

Secondly observe closely in code I have written findgameobjectswithtag this will find all game objects with specified tag.

Step2:Save your script and head on to unity assign all the game objects you want to find with specified tag and click on play.



Array is initialized with size of 3 elements and is assigned 3 game objects automatically.
Size is initialized at runtime. It can be 3 or more than 3 or less than 3 depends on the GameObject script finds with specified tag.
Axact

İslevselBilgi

Emre Oy Tarafından Tasarlanmıstır

Post A Comment:

0 comments: