如何在 Unity C# 中仅使用脚本中的游戏对象启用部件/组件 [英] How do I enable Parts/Components in Unity C# with only a game object in the script
问题描述
我正在使用 Photon 在我的游戏中加入多人游戏,以确保一个玩家不会控制所有玩家,当您生成时,客户端会激活您的脚本/相机,以便您可以看到和移动.
I am using Photon to make put multiplayer in my game, to ensure that one player doesn't control them all, when you spawn in, client side it will activate your scripts/camera so you can see and move.
虽然我想不出解决这个问题的方法,因为我不知道如何启用/禁用孩子的组件或启用孩子的孩子.
Although I can't think of a way around this problem, since I don't know how to enable/disable children's components or enable a child's child.
我想通过脚本来启用它http://imgur.com/ZntA8Qx
我的脚本是这样的:
using UnityEngine;
using System.Collections;
public class NetworkManager : MonoBehaviour {
public Camera standByCamera;
// Use this for initialization
void Start () {
Connect();
}
void Connect() {
Debug.Log("Attempting to connect to Master...");
PhotonNetwork.ConnectUsingSettings("0.0.1");
}
void OnGUI() {
GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
}
void OnConnectedToMaster() {
Debug.Log("Joined Master Successfully.");
Debug.Log("Attempting to connect to a random room...");
PhotonNetwork.JoinRandomRoom();
}
void OnPhotonRandomJoinFailed(){
Debug.Log("Join Failed: No Rooms.");
Debug.Log("Creating Room...");
PhotonNetwork.CreateRoom(null);
}
void OnJoinedRoom() {
Debug.Log("Joined Successfully.");
SpawnMyPlayer();
}
void SpawnMyPlayer() {
GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate("Body", Vector3.zero, Quaternion.identity, 0);
standByCamera.enabled = false;
((MonoBehaviour)myPlayerGO.GetComponent("Movement")).enabled = true;
}
}
带有 monobehaivour 的东西下方底部的位是我想要启用它们的地方如您所见,我已经想出了如何激活属于我生成的游戏对象的一部分,我只是需要我上面所说的帮助,谢谢您的帮助.
The bit at the bottom underneath the thing with monobehaivour is where I want to enable them As you can see I have already figured out how to activate something that is part of the game object I spawned, I just need help with what I said above, thank you for your help.
我通过预制件生成它,所以我希望它只编辑我生成的那个,而不是关卡中的所有其他的,因为我想使用 myPlayerGO Game 对象启用这些组件,然后那个仅.
I'm spawning it through a prefab, so I want it to edit only the one I spawn, and not every other one in the level, as in I want to enable these components using the myPlayerGO Game object, and that one only.
这是让我的游戏运行所需的全部内容,所以请帮忙.
This is all I need to get my Game working, so please help.
如果这是重复的,我很抱歉,因为我不知道如何表达这个标题.
If this is a duplicate, I'm sorry because I wasn't sure how to word the title of this.
推荐答案
I unity you can enable and disable components in child object by using gameObject.GetComponentInChildren
I unity you can enable and disable components in child object by using gameObject.GetComponentInChildren
ComponentYouNeed component = gameObject.GetComponentInChildren<ComponentYouNeed>();
component.enabled = false;
也可以使用gameObject.GetComponentsInChildren
这篇关于如何在 Unity C# 中仅使用脚本中的游戏对象启用部件/组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!