Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Fox-Handler · May 12, 2014 at 08:55 AM · gameobject.findgameobject

GameObject.FindGameObjectWithTag

Hi I was wondering what the difference was between two lines of code. In the first script it says theBall = GameObject.FindGameObjectWithTag("Ball").transform; but in the second script it says if(!GameObject.FindGameObjectWithTag("Music")) etc.. So what is the difference? Why do I have to put transform at the end of one line of code and not the other. Also isn't the variable in function Start() supposed to be out of scope since it is inside a function?

 static var playerScore01 : int = 0;        
 static var playerScore02 : int = 0;
 
 var GuiSkin1 : GUISkin;
 var theBall : Transform;
 
 function Start () 
 {
     theBall = GameObject.FindGameObjectWithTag("Ball").transform; 
     
 }
 
 static function Score (wallName : String)                 
 {
     if(wallName == "rightWall")
     {
         playerScore01 += 1;
     }
     else
     {
         playerScore02 += 1;
     }
     
 }
 
 function OnGUI () 
 {    
     GUI.skin = GuiSkin1;
                                       
     GUI.Label (new Rect (Screen.width/2 - 150 - 18, 20, 100, 100),"" + playerScore01);    
     GUI.Label (new Rect (Screen.width/2 + 150 - 18, 20, 100, 100),"" + playerScore02); 
     
     if (GUI.Button ( new Rect (Screen.width/2-121/2, 35, 121, 53), "RESET"))    //Screen.Width/2-121/2 because anchor point starts at top left corner...
     {
         playerScore01 = 0;
         playerScore02 = 0;
         
         theBall.gameObject.SendMessage ("ResetBall");
         
     }
 
 }

&&

 var musicPrefab : Transform; 
 function Start () 
 {
     currentScore = 0;
     
     if(!GameObject.FindGameObjectWithTag("Music"))
     {    
         var mManager = Instantiate (musicPrefab, transform.position, Quaternion.identity)
         mManager.name = musicPrefab.name; 
         DontDestroyOnLoad (mManager); 
     }
 }
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

3 Replies

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

Answer by oliver-jones · May 12, 2014 at 09:22 AM

Well, both scripts appear to do something completely different to one another.

The first script will find a GameObject tagged 'Ball', and reference it as a Transform in a variable called 'theBall'.

The second script will find a GameObject tagged 'Music', and not reference it at all.

The GameObject is the actual object you see in the game scene, this is the GameObject - the physical being of the object. Attached to GameObjects are other components, such as the Renderer (how it looks), maybe a Rigidbody (how it collides), and a Transform (its position/rotation/scale).

You can reference it as a GameObject if you want:

 var theBall : GameObject;

But if you only want to talk to the transform of it (maybe change the scale), then you will need to tell unity that - by added '.transform' at the end:

 theBall.transform.localScale.x = 2;

Notice how I added the '.transform'? Thats because I'm referring to the Transform of the GameObject only. If I had the variable as a Transform, then I don't need to refer to it again:

 var theBall : Transform;
 theBall.localScale.x = 2;

Notice how I don't need the '.transform' anymore? Thats because I'm already referring to the Transform, because I've declared the variable as a Transform type.

Now - In terms of having a variable inside, or outside a function - that depends on what your doing with it. Odds are, you'll need it outside the function. Example:

 function Start(){
     
     var Player = GameObject.FindGameObjectWithTag('player');
 }
 
 function Update(){
     
     Player.transform.localScale.x = 2;
 }

This will throw an error, because the Update function is trying to find a variable called 'Player', which does not exist - it only exists within the Start function - see it as a type of 'localised' variable only 'visible' to that function - in this case, the Start function.

If you'll have a load of functions using this variable, then yes, you do want to put it outside:

 var Player : Transform;
 
 function Start(){
     
     Player = GameObject.FindGameObjectWithTag('player').transform;
 }
 
 function Update(){
     
     Player.localScale.x = 2;
 }
 
 function KillPlayer(){
     
     Destroy(Player.gameObject);
 }

A few things to notice here, I now have the variable outside, so now all functions can 'see' it, and use it. I've defined the variable as a Transform, so in the Start function, I need to add '.transform' at the end as the 'GameObject.FindGameObjectWithTag' returns the GameObject, the my variable is expecting it's Transform.

Also, the Update function: I don't need to include the '.transform' as it already knows its a Transform. Although, in the KillPlayer function, I have 'Player.gameObject'.

The Destroy method expects a GameObject, so I need to add '.gameObject' at the end because Player alone is a Transform, remember?

Hope this clears things up:

  • Item.gameObject - return the GameObject

  • Item.transform - return the Transform

  • Item.gameObject.transform - return the Transform

  • Item.gameObject.transform.gameObject - return the GameObject

  • Item.transform.gameObject - return the GameObject

You can even have something like this:

Item.gameObject.transform.gameObject.transform.gameObject.transform.gameObject, which will return the gameObject - but this is ridiculous.

Comment
Add comment · Show 1 · 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 Fox-Handler · May 12, 2014 at 09:57 AM 0
Share

Great! thank you, it is a lot clearer. At first I just thought the "type" was put there to keep the machine from throwing an error. I wasn't aware how it was used. thanks for clearing that up.

avatar image
0

Answer by HarshadK · May 12, 2014 at 09:09 AM

The variable theBall is of type Transform. So when you are using

 theBall = GameObject.FindGameObjectWithTag("Ball").transform;

you are finding the game object Ball and storing its transform into theBall variable.

If theBall variable was of type GameObject then you could have written

 var theBall: GameObject;
 theBall = GameObject.FindGameObjectWithTag("Ball");

And in second case where you are writing

 if(!GameObject.FindGameObjectWithTag("Music"))

you are checking for if the game object tagged Music is present or not and not checking for the transform of the Music game object that's why transform is not used in the second case.

Just so you know when you are assigning the value to you musicPrefab variable which is of type Transform you will write

 musicPrefab = GameObject.FindGameObjectWithTag("Music").transform;

So that you will store the value of transform of Music game object in musicPrefab.

Comment
Add comment · Show 1 · 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 Fox-Handler · May 12, 2014 at 09:51 AM 0
Share

thanks I decided to change .transform to .gameobject ins$$anonymous$$d since it seems a little more straightforward.

avatar image
0
Wiki

Answer by Kiwasi · May 12, 2014 at 09:13 AM

As to your question there is no need to use the Transform in the first set of code.

Try changing line 5 to

 var theBall : GameObject;

And line 9 to

 theBall = GameObject.FindGameObjectWithTag("Ball");

This should produce identical results.

Some thoughts on Transform versus GameObject:

Transform is a component on the GameObject. The unique thing about a Transforms is that every GameObject must have one and only one.

Some of unity's methods will implicitly convert a GameObject into a Transform, or vice versa. Check out the scripting reference for details. You can always explicitly convert between the two as follows.

 // To get the GameObject
 transform.gameObject;
 //To get the Transform
 gameObject.transform;

The main reason you might want to grab a transform instead of a GameObject is if you want to affect position directly.

The main reason you might want to grab a GameObject instead of a Transform is if you want to affect other components on the GameObject

Edit: theBall is not out of scope because it is declared on line 5

Comment
Add comment · 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

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

23 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

Related Questions

Get a parent GameObject from a class on a script attached to a prefab,Getting a parent Gameobject from a component on a script attached to a prefab 1 Answer

Build does behave different to editor 2 Answers

Get list of gameObjects and change booleans within them 2 Answers

All instances of a prefab effected by single access -- no Static variables 1 Answer

Find a GameObject without a reference 2 Answers


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