简体   繁体   English

如何从另一个GameObject实例化a:monobehaviour脚本

[英]How to instantiate a : monobehaviour script from another GameObject

I'm having trouble figuring this one out and am finding little on google that helps me. 我很难搞清楚这一点,并且在谷歌上发现很少帮助我。

In unity, if a script derives from MonoBehaviour, it cannot be instantiated using the "new" keyword. 在统一中,如果脚本派生自MonoBehaviour,则无法使用“new”关键字对其进行实例化。 Fine. 精细。 So I've been looking at how to do this, and it seems that getting the GameObject, then getting it's GameComponent is the way to go... But it's not going very far.. 所以我一直在研究如何做到这一点,似乎获得GameObject,然后得到它的GameComponent是要走的路......但它不会走得太远......

So I have a GameObject called NetworkManager. 所以我有一个名为NetworkManager的GameObject。 Attached to it is a script called NetworkManager. 附加到它的是一个名为NetworkManager的脚本。 I have another GameObject called Main, attached to it is a script called Main (surprise!). 我有一个名为Main的另一个GameObject,附加到它的是一个名为Main的脚本(惊喜!)。 Basically what I want to do is "instantiate" the network manager within the main script, so that i can do things like networkManager.hostServer() or networkManager.kickPlayer() and so on. 基本上我想要做的是在主脚本中“实例化”网络管理器,以便我可以执行诸如networkManager.hostServer()或networkManager.kickPlayer()等操作。

This is my main.cs script: 这是我的main.cs脚本:

public class Main : MonoBehaviour {

Player player;
GameObject networkManager;
UiManager uiManager;

// Use this for initialization
void Start () 
{

    player = new Player(Network.player, Environment.UserName, 0, 0);
    networkManager = GameObject.FindGameObjectWithTag("NetworkManager").GetComponent<NetworkManager>();
    uiManager = new UiManager();

}

The error I am getting is as follows:" Cannot implicitly convert type 'Assets.scripts.NetworkManager' to 'UnityEngine.GameObject' " 我得到的错误如下:“无法将类型'Assets.scripts.NetworkManager'隐式转换为'UnityEngine.GameObject'”

The problem arises when I add the ".GetComponent();" 当我添加“.GetComponent();”时出现问题。 part. 部分。 Now it seems obvious that if the function is looking for a Component type and is instead getting a NetworkManager type that it wouldn't work... And that's where I'm stuck. 现在看来很明显,如果函数正在寻找一个组件类型,而是获得一个不起作用的NetworkManager类型......那就是我被困住的地方。 I have no idea how to get this to work, and I've surpassed my 15 minute limit on google searching. 我不知道如何让这个工作,我已经超过了谷歌搜索的15分钟限制。

If you want to instantiate new Network manager GameObjects you should use some time to get to know "prefabs". 如果要实例化新的网络管理器GameObjects,您应该花一些时间来了解“预制件”。 You can make your Network manager GameObject into one of those by dragging it from your scene to one of your folders. 您可以通过将网络管理器GameObject从场景拖动到其中一个文件夹,将其作为其中之一。 Instantiating a prefab is as simple as: 实例化预制件非常简单:

 Instantiate(NetworkManagerPrefab, new Vector3(0, 0, 0), Quaternion.identity);

This will create new NetworkManager GameObject into your scene. 这将在场景中创建新的NetworkManager GameObject。

As you said, your script is not working because it is getting the component and storing it to GameObject. 正如您所说,您的脚本无法正常工作,因为它正在获取组件并将其存储到GameObject。 Just change the type of the variable to the type of the component or if the component is a script to the type of the class. 只需将变量的类型更改为组件的类型,或者组件是类的类型的脚本。 In your case change GameObject networkManager; 在你的情况下改变GameObject networkManager; to NetworkManager networkManager; NetworkManager networkManager; and it will work. 它会起作用。

If you have the prefab then you can instantiate it. 如果你有预制件,那么你可以实例化它。

GameObject g = Instantiate(prefab, position, rotation) as GameObject;

If the GameObject is already in the scene don't instantiate it unless you want more than one. 如果游戏对象已经在场景中,则不要实例化它,除非您想要多个游戏对象。 You can just find it. 你可以找到它。

GameObject g = GameObject.Find("GameObject Name");

Then you will ask, how do I get the script attached to the GameObject. 然后你会问,我如何将脚本附加到GameObject上。 Like this: 像这样:

ScriptName s = g.GetComponent<ScriptName>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM