- Home /
Syntax for a game object component variable on a transform in an FBX hierarchy?
I have a known incoming transform hierarchy from Maya like this:
root|one|two|three
If I want to refer to a variable on transform three in Maya I would use:
root|one|two|three|threeShape1.var
After importing into Unity as an FBX how do I refer to a public game object component variable on transform three without sorting through everything in the scene using "find"?
Thanks!
Feb26: Wow - is this too hard? What I was expecting is something like:
transform(root/one/two/three/).gameObject.componentName.variableName
Maybe the question should be:
How do you refer to a unique game object component on a unique child transform? (From a script component on the root transform)
Do you really have to maintain a hash table of all your objects? Shouldn't Unity do that? http://answers.unity3d.com/questions/11959/transform-find-and-instantiate-do-not-return-the-same-type
I'm completely confounded by the relationship between a transform and a game object. All 3D software uses transform hierarchies as the basic organisational structure - is Unity some how different??? http://unity3d.com/support/documentation/ScriptReference/Transform.html http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponent.html
A GameObject is the Parent, requiring a Transform Component. When beco$$anonymous$$g a child of another GameObject, it becomes a Transform-GameObject you can't access through Components, for some obscure reason.
Any time you'll be accessing a Transform/Component/GameObject more than once, I recommend storing it in a variable/hashtable/array.
Thanks -but if you know what it is called and that the name is unique why do you need to parse through every object in the scene to search for it? Seems like a waste of resources and unnecessarily complex?
Answer by by0log1c · Feb 26, 2011 at 05:49 AM
I couldn't tell for C#, but JScript goes something like this. I used this to retrieve the hand of my character so I could AddMixingTransform to an animation. Store the reference in a variable if you are to call this often.
var theHand = gameObject.transform.Find("Model1/Body2/Hand3");
That'll consider parent GameObject as the current root. Tweak around to get what you need.
EDIT: Or a long example for accessing a variable into a script from a child GameObject/Transform.
var theHandVariable = gameObject.transform.Find("Model1/Body2/Hand3").GetComponent("ScriptName").aVariable;
Once again, .Find and .Get functions are slow, cut out each one you can by storing in a variable!
Interesting! Thanks for your input. Is there no way around using 'find'? I don't want to have to search for an object if I know it exists. I've read that searching with 'find' can slow down games. Is there no way to directly refer to a transform(game object?) component without searching for it? Does unity not keep track of what is in a scene? http://forum.unity3d.com/threads/67943-What-can-slow-down-your-games "Avoid slow functions (like FindObject)" Thank you
Slow reply,sorry. I also corrected the code a lil' bit. I'll also answer because I care: .Find functions are considered slow, the idea to avoid them is to use them once, then store the result in a variable. What's to avoid is to repeatedly .Find for a hand that most likely never changes. Using a variable is fine :)
Thanks for your input BY0LOG1C - could you add the code for changing a variable that you deleted as a note? It was very useful and I didn't make a copy! I still can't believe you can't refer to elements without having to search for them. I guess the most efficient way is to have a public variable and drag and drop.
Your answer
Follow this Question
Related Questions
Transform.gameObject References Itself?! 0 Answers
Why is there no gameObject.parent? 1 Answer
Selecting transform in FBX hierarchy csharp script 1 Answer
A GameObject That's Not Active In The Hierarchy Still Calling Its Start Method? 1 Answer
I want to move a cube with rotation but I find this problem 1 Answer