我如何能在Unity C#零件/元件仅在脚本中的游戏对象 [英] How do I enable Parts/Components in Unity C# with only a game object in the script

查看:276
本文介绍了我如何能在Unity C#零件/元件仅在脚本中的游戏对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的光子,使把多人在我的游戏,保证一个玩家不能控制他们,当你在产卵,客户端会激活你的脚本/摄像头所以你可以看到和移动。

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

I want to enable this through scripting http://imgur.com/ZntA8Qx

和本
http://imgur.com/Nd0Ktoy

我的脚本是这样的:

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.

我产卵它通过prefab,所以我想它来编辑的水平只有一个我产卵,而不是每隔一个,因为在我想启用使用myPlayerGO游戏对象这些组件,并且一个只。

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.

推荐答案

我合一您可以启用和使用禁用子对象组件的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆