- Home /
How to enable component of children in instantiated prefab?
I'm following Quill18's Multiplayer FPS Tutorial and in Part 5 we have to enable some components in a instantiated player prefab. However, in my particular case, the player itself (with "MouseLook", "FPSInputController" and "CharacterMotor" scripts and such) is a children of another GO (caller Player GO).
How can I enable these 3 italicized components, since they're the components of a children object?
Bear in mind that these 3 scripts are JS, whereas the script that instantiates the player and enable these components is C#.
Below there is my hierarchy for the player GO and script that instantiates players:
using UnityEngine;
using System.Collections;
public class NetworkManager : MonoBehaviour {
public GameObject standbyCamera;
SpawnSpot [] spawnSpots;
// Use this for initialization
void Start () {
spawnSpots = GameObject.FindObjectsOfType<SpawnSpot> ();
Connect ();
}
void Connect () {
PhotonNetwork.offlineMode = false;
PhotonNetwork.ConnectUsingSettings ("RPGVR 1.0.0");
}
void OnGUI (){
GUILayout.Label (PhotonNetwork.connectionStateDetailed.ToString() );
}
void OnJoinedLobby() {
PhotonNetwork.JoinRandomRoom ();
}
void OnPhotonRandomJoinFailed() {
PhotonNetwork.CreateRoom (null);
}
void OnJoinedRoom(){
Debug.Log ("OnJoinedLobby");
SpawnMyPlayer ();
}
void SpawnMyPlayer (){
if (spawnSpots == null) {
Debug.LogError ("No Spawn Spots avaiables. Don't Panic");
return;
}
SpawnSpot mySpawnSpot = spawnSpots[ Random.Range (0, spawnSpots.Length) ];
GameObject myPlayerGO = (GameObject) PhotonNetwork.Instantiate("Player GO", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);
//Disable the Standby Camera
standbyCamera.SetActive(false);
//Enable "Sky Dome" Children of "PlayerGO" Game Object
myPlayerGO.transform.FindChild("Sky Dome").gameObject.SetActive(true);
//Enable Components in "Player" Children of "PlayerGO" Game Object
((MonoBehaviour)myPlayerGO.transform.FindChild("Player").GetComponent("MouseLook")).enabled = true;
// ((MonoBehaviour)myPlayerGO.GetComponent ("FPSInputController")).enabled = true;
// ((MonoBehaviour)myPlayerGO.GetComponent ("MouseLook")).enabled = true;
// ((MonoBehaviour)myPlayerGO.GetComponent ("CharacterMotor")).enabled = true;
// myPlayerGO.transform.FindChild("Camera 1").gameObject.SetActive(true);
}
}
Please help!
Answer by SkaredCreations · Dec 31, 2014 at 12:03 PM
If you want that the C# scripts access to JS scripts, then you have to put the JS scripts into a folder called "Standard Assets". This way you can do in C#:
yourPlayerGo.GetComponent<YourJsScript>().enabled = true;
Usually mixing JS and C# is not a very good idea, because one of them must be placed in "Standard Assets" and this means that you can access these scripts only one-way (C# to JS or JS to C#). I would suggest to leave convert all the scripts to C# or JS and have one only script type in your project, will be much easier to maintain.
Thank you Skared, that does works. I end up following your advice and now its all in C#! =) However I still got problems with this script (but not regarding those 3 components) wich I shall ask in another question.
Your answer
![](https://koobas.hobune.stream/wayback/20220613175228im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Enable Child Component C# 1 Answer
Collider.enabled working differently from inspector? 1 Answer
Enable/Disable a Component by using a String Variable for the Component name 2 Answers
Component name variable 2 Answers
Unable to enable script 0 Answers