- Home /
Was gameObject.GetChild(childsName); a thing?
In my project I am going through old code converting our networking solution from Tnet to Photon. Up until just now these lines did not throw an error and worked.
login = displayInst.gameObject.GetChild("LoginScreen");
normal = displayInst.gameObject.GetChild("NormalScreen");
After making my networking changes I started getting an error saying :
Type UnityEngine.GameObject' does not contain a definition for GetChild' and no extension method
GetChild of type UnityEngine.GameObject could be found (are you missing a using directive or an assembly reference?)
I have not made any changes to those lines. After some research all I'm seeing the closest thing could be gameObject.Transform.GetChild(index);
Opened the script on another computer without any of the networking changes and MonoDevelop would still AutoComplete gameObject.GetChild(string);
Clearly it's not a thing now, but was it maybe a few updates ago? Why would my script compile and run just fine before making our networking changes? Working around the issue but still very confused as to what is going on....
attached a screenshot just to prove it
Are you sure you haven't deleted some extension method of your own? I have no recollection of this function, and at least checking back to 4.0 documentation doesn't seem to turn anything up. For the same functionality (using a string) you can use Transform.Find(someString) to find children by string.
Went back and double checked apparently Tnet wrote an override method for gameObject. As soon as I added the namespace back it worked. strange
Parenting structure is actually handled by the Transform. As every gameobject has a Transform this is not a problem.
GameObject.Find searches the Root and all its children (so, every gameobject) and Transform.Find will search only the children of that one object. GetChild is also a Transform $$anonymous$$ethod as well as other useful $$anonymous$$ethods.
Answer by B_Dombro · May 03, 2016 at 06:24 PM
Turns out gameObject.GetChild("object name"); is a valid method when using the Tnet namespace.
Answer by Dave-Carlile · May 03, 2016 at 12:18 PM
Doing a quick Google search for Unity3D "GameObject.GetChild" yielded up this on the Unity wiki...
http://wiki.unity3d.com/index.php/VWheelCollider
About 60% of the way down you'll find...
public static class ClassExtensions
{
// GameObject
public static GameObject GetChild(this GameObject obj, string name) { return obj.transform.FindChild(name) ? obj.transform.FindChild(name).gameObject : null; }
Which creates a GameObject extension. This is probably what your old project had in it somewhere, or some equivalent extension. It's possible that it got blown away by something when you made your changes.
I don't think you need the FindChild thing anymore, in fact that may be deprecated, or at least there were some changes around those find functions awhile back - don't recall the details.
Your answer
Follow this Question
Related Questions
How do I fix this problem-it won't let me start my game in Unity because of an override problem 1 Answer
How do I inherit certain varibles and functions from another script. 1 Answer
Error "OnTriggerEnter: this message has to be of type: Collider" 0 Answers
Script Error 1 Answer
error CS0117: `UnityEngine.Graphics' does not contain a definition for `DrawProceduralIndirectNow' 0 Answers