- Home /
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?
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
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.
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;
}
Your answer
Follow this Question
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