- Home /
Activating a child object in C#
Complete newbie with C#, but I'm trying to use Photon Cloud and this is looking like the only way to go. My problem is this: I can't find a way to activate the camera on an instantiated first person controller. I get an "error CS0246: The type or namespace name 'MainCamera' could not be found. Are you missing a using directive or assembly reference?"
My code: void OnJoinedRoom(){
GameObject Avat = PhotonNetwork.Instantiate ("myFPC", new Vector3(0,2,0), Quaternion.identity, 0);
MainCamera currentCam = Avat.Find("Main Camera");
currentCam.active = true;
}
Answer by kmeboe · Sep 06, 2012 at 06:19 PM
I'm not familiar with Photon Cloud, but I'm guessing "Main Camera" is an object in your scene? In that case, it's probably just a "GameObject". Try this:
void OnJoinedRoom(){
GameObject Avat = PhotonNetwork.Instantiate ("myFPC", new Vector3(0,2,0), Quaternion.identity, 0);
Transform currentCam = Avat.transform.Find("Main Camera");
currentCam.active = true;
}
For more information about accessing objects from other objects in Unity, see here: http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
That does something at least :) Unfortunately, now I have a new error message:
"Static member 'UnityEngine.GameObject.Find(string)' cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d"
Any ideas on this one? And thank you for helping!
You're welcome; hopefully we'll get you there. :)
Try "Avat.transform.Find" ins$$anonymous$$d. I've updated the above code to match.
That didn't quite fix it, but it did give me enough information that I could :D Swapped the code to this:
Transform currentCam = Avat.transform.Find("$$anonymous$$ain Camera")
Then it worked :) Now on to the next problem. Thanks a bunch!
Oh, and can you update your code with that in case anyone else shows up to look at this?