I have hierarchy with a camera at the top. When I do Code (csharp): temp as duck Debug.Log(transform.root) temp = transform.root Debug.Log(temp) Debug.Log(temp.WorldToViewportPoint(transform.position)) (temp as duck is just boo's way making an untyped variable) temp is the transform attached to the camera. I would like to get the object ( a camera) that the transform is attached to. So I can use WorldToViewportPoint method. The above code fails because temp is a transform not the object the transform is attached to. I also tried temp = transform.root.parent but it has no effect. Is there a way to get the object a transform is attached to?
I think what you are looking for is the inherited gameObject class. whatever.transform.gameObject; -Jeremy
Thanks. I tried Code (csharp): temp as duck Debug.Log(transform.root) temp = transform.root.gameObject Debug.Log(temp.name) Debug.Log(temp.WorldToViewportPoint(transform.position)) but at runtime it still can not find method WorldToViewportPoint. The name printed out is the cameras name.
That's because GameObject doesn't have a WorldToViewPortPoint function. That function is in the camera class. You need transform.root.gameObject.camera; -Jeremy
Thanks both those work. When I look at inspector it is obvious that when I choose create camera, I create a gameObject that has a camera and a transform attached. I guess the using x.camera instead of x.gameObject.camera is a convience shortcut Cheers Grant
Np. If you look at the script reference for the class in question, you can see what members and methods are inherited from other base classes. Saves you doing GetComponent and such yourself when you really dont need to. -Jeremy