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 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 this method works?

I think the methods I've tried that spat it back out were .Transform and .GetComponent(Transform).

also just as an extra query whilst 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
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
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 "hit" 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 this to other GameObject variables:

 if(gameObject==myVariableNameContainingTheObject)
     // do something
 

Note, everything is case sensitive, so .Transform won't work, use .transform

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

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

 var hit: RaycastHit;
 var newThing;
 
 if (Physics.Raycast(ray, hit)) {
     newThing = Instantiate(prefab, hit.point, Quaternion.identity);
     var pos = newThing.transform;
 
 }
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

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

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

Souls pickup mechanic (Like Dark Souls, BloodBourne) 0 Answers

Instantiate inactive object 4 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