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 KhaLa · Oct 01, 2012 at 10:08 AM · c#gameobjectgeneral

[Simple] C# Retrieving the game object that the script is called.

Greetings,

I've been using C# only and I'm wondering if we can retrieve the game object that the script is called.

What I mean by that is that in my program I've "assigned" (if that's what it is called) the same script to multiple different game objects in my program and in the code I want to retrieve which object is currently executing the code (every object will execute the same code in their own ways when the time comes).

I've solved the problem by doing something like this:

 // This object is a prefab (using default values from assets)
 public Transform objectO;
 
 // I drag and drop the following from the IDE
 public Transform currentGameField;
 // I do that to every object that the script is assigned.
 
 void OnMouseDown(){
    //Scaling
    objectO.localScale = new Vector3(3.5f, 3.5f, 3.5f);
    // Some more stuff here, skipped
    translateObjectO();
 }
 
 void translateObjectO(){
    Vector3 tempPosition = currentGameField.localPosition;
    switch(regionNumber) {
    case 1:
       tempPosition.y = objectO.localPosition.y + 0.09f;
       break;
    // Deleted the fallowing code, unecessary here
    case 2:
       break;
    case 3:
       break;
    case 4:
       break;
    case 5:
       break;
    case 6:
       break;
    }
    // Finally assign back to the object
    objectO.localPosition = tempPosition;
 }

The problem is that I don't want to drag the game object to currentGameField hundred times. I want to access the object something like:

`this.Transform.localPosition`

where both I don't store the object and don't assign it from IDE. So I want to remove the `currentGameField` in the above code.

Thanks in advance,

Metan

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
0

Answer by whydoidoit · Oct 01, 2012 at 10:30 AM

The transform variable contains the transform of the object that the script is attached to, gameObject contains the GameObject. See the inherited variables here for details of all the other things.

It is however worth caching transform in Awake or Start as this yields faster code.

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

10 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

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

gameObject are not referenced 2 Answers

Ordering a list of GameObjects 3 Answers

How to scroll progressively a game object? 0 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