Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

transform.Find not working? could be something else. I need a smart person asap!

Discussion in 'Scripting' started by keenanwoodall, Jun 2, 2014.

  1. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    If anyone is familiar with quill18 multiplayer tutorial on youtube, that's what my project is based around. However, instead of using the default fps controller, I'm using the sample asset's first person controller. It has the head bob. I love the head bob but my Camera is nested below it and I can't access it through script, also everything below the first tier of children doesn't show in the project view. Here's my code: By the way, I also tried a direct reference and that didn't work. ie: ("HeadJoint/FirstPersonCamera"). My player being instantiated from a prefab and doesn't exist in the scene unity the player joins a room.
    Code (csharp):
    1.  
    2. public class NetworkManager : MonoBehaviour
    3. {
    4. public Camera standbyCamera;
    5. scriptSpawnSpot[] spawnSpot;
    6. void Start ()
    7. {
    8. spawnSpot = GameObject.FindObjectsOfType<scriptSpawnSpot>();
    9. Connect ();
    10. }
    11. void Connect ()
    12. {
    13. PhotonNetwork.ConnectUsingSettings("FPS v1.0.0");
    14. //PhotonNetwork.offlineMode = true;
    15. }
    16. void OnGUI ()
    17. {
    18. GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
    19. }
    20. void OnJoinedLobby ()
    21. {
    22. PhotonNetwork.JoinRandomRoom();
    23. Debug.Log ("Searching for a room");
    24. }
    25. void OnPhotonRandomJoinFailed ()
    26. {
    27. PhotonNetwork.CreateRoom(null);
    28. Debug.Log ("No room available, creating a room");
    29. }
    30. void OnJoinedRoom()
    31. {
    32. Debug.Log ("Joined a room");
    33. SpawnMyPlayer ();
    34. }
    35. void SpawnMyPlayer ()
    36. {
    37. if (spawnSpot == null)
    38. {
    39. Debug.Log("Aah, hell nah!");
    40. }
    41. scriptSpawnSpot mySpawnSpot = spawnSpot[Random.Range(0,spawnSpot.Length)];
    42. Debug.Log ("Spawned at " + mySpawnSpot);
    43. GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate("Player",mySpawnSpot.transform.position,mySpawnSpot.transform.rotation,0);
    44. standbyCamera.enabled = false;
    45. myPlayerGO.GetComponent<FirstPersonCharacter>().enabled = true;
    46. myPlayerGO.GetComponent<SimpleMouseRotator>().enabled = true;
    47. myPlayerGO.GetComponent<FirstPersonHeadBob>().enabled = true;
    48. myPlayerGO.transform.Find("FirstPersonCamera").gameObject.SetActive(true);
    49. }
    50. }
    51. [CODE]
    transform_Find_error.png
     
    Last edited: Jun 2, 2014
  2. SchneiderAtvis

    SchneiderAtvis

    Joined:
    Feb 19, 2013
    Posts:
    38
    Please use code tags and you need to show us the hierarchy of myPlayerGO.
     
  3. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    alrighty, there ya go guys
     
  4. DScarpelli

    DScarpelli

    Joined:
    Sep 23, 2010
    Posts:
    17
    Try this...

    In your prefab, instead of turning off the whole FirstPersonCamera game object, just uncheck its Camera component.

    Then ...
    GameObject.Find("FirstPersonCamera").GetComponent<Camera>().enabled = true;
     
  5. SchneiderAtvis

    SchneiderAtvis

    Joined:
    Feb 19, 2013
    Posts:
    38
    Looks like there are spaces in the gameobject names have you tried Find with this string?:
    Code (csharp):
    1. "Head Joint/First Person Camera"
     
  6. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    Hmm, I tried it and still got the same error. this is weird lol
     
  7. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    I figured it out, I needed to put:
    Code (csharp):
    1. myPlayerGO.transform.FindChild("Head Joint/First Person Camera").gameObject.SetActive (true);[CODE]