• 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
Question by KennSan · May 25, 2011 at 07:38 PM · gameobjectinstantiatemouse

getting .Transform from instanntiated object

I've been looking at the code for clicking on a game object by using ray cast here http://forum.unity3d.com/threads/31708-Detecting-an-object-from-a-point-on-the-screen

my only issue is the objects are instantiated, they're in variables upon instantiation, I'm just curious if anyone can tell me is there any way to call upon the Transform of an object through scripting so t$$anonymous$$s method works?

I t$$anonymous$$nk the methods I've tried that spat it back out were .Transform and .GetComponent(Transform).

also just as an extra query w$$anonymous$$lst I'm referring to getting information from game objects. Is it possible for a specific game object to check if it is the object in a GameObject variable?

Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Wolfram · May 25, 2011 at 08:01 PM

I don't fully get what you are asking in the first part of your question, since andeeee's example script doesn't contain any object references, except in the "$$anonymous$$t" structure returned by Physics.Raycast.

To access the transform of the GameObject your script is attached to, you can use gameObject.transform, or even just transform:

 void Start(){
     print("My world position is "+transform.position);
 }

If you have a GameObject in a variable - no matter whether it's instantiated, assigned in the Inspector, or even found using scripted means (such as GameObject.Find(...)), the transform is accessed the same way: myVariableNameContainingTheObject.transform

For the second part of your question, the GameObject a script is attached to can be accessed via "gameObject". So you can simply compare t$$anonymous$$s to other GameObject variables:

 if(gameObject==myVariableNameContainingTheObject)
     // do somet$$anonymous$$ng
 

Note, everyt$$anonymous$$ng is case sensitive, so .Transform won't work, use .transform

Comment
KennSan

People who like this

1 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 KennSan · May 25, 2011 at 08:16 PM 0
Share

Brilliant - this seems to have sorted both my queries out! =) you're a star - unfortunately I fall more under the design sector of games design as opposed to tech so sometimes when it comes to coding I fall a little slow on the mark.

Usually when I get a little stuck my google-fu comes in handy but sadly not on this occasion.

avatar image

Answer by DaveA · May 25, 2011 at 07:58 PM

 var $$anonymous$$t: RaycastHit;
 var newT$$anonymous$$ng;
 
 if (Physics.Raycast(ray, $$anonymous$$t)) {
     newT$$anonymous$$ng = Instantiate(prefab, $$anonymous$$t.point, Quaternion.identity);
     var pos = newT$$anonymous$$ng.transform;
 
 }
Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Continually Updating position of Instantiated Object (to Mouse Pointer) 0 Answers

prefab is instantiating without a script 2 Answers

How can I check if an instantiated object collides with another instantiated object? 1 Answer

Cannot cast from source type to destination type 1 Answer

How to rotate an instantiated object by a fixed amount before it is spawned 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