Types 无法从用法推断方法的类型参数

Types 无法从用法推断方法的类型参数,types,unity3d,arguments,Types,Unity3d,Arguments,这是我的代码,我正试图找出这一行的问题所在 实例=新游戏对象(“游戏状态”).AddComponent() 下面是我得到的错误:无法从用法推断方法“UnityEngine.GameObject.AddComponent()”的类型参数。尝试显式指定类型参数 using UnityEngine; using System.Collections; public class gamestate : MonoBehaviour { //Declare Properties priv

这是我的代码,我正试图找出这一行的问题所在

实例=新游戏对象(“游戏状态”).AddComponent()

下面是我得到的错误:
无法从用法推断方法“UnityEngine.GameObject.AddComponent()”的类型参数。尝试显式指定类型参数

using UnityEngine;
using System.Collections;

public class gamestate : MonoBehaviour
{

    //Declare Properties
    private static gamestate instance;

    public static gamestate Instance
    {
        get
        {
            if(instance == null)
            {
                instance = new GameObject( "gamestate").AddComponent ();
            }

            return instance;
        }
    }    

    public void startState()
    {
        print ("Creating a new game state");
    }
}
这一行:

instance = new GameObject( "gamestate").AddComponent ();
具体而言,这一呼吁:

AddComponent ();
您想添加什么类型的组件?假设您要添加一个
音频源

AddComponent<AudioSource>();

为您自己的引用,这些调用与C++中的模板类似。


另外,常见的做法是将类名大写(如
Gamestate
Gamestate
)。编译器不会在意,但其他开发人员可能会在意。

我想我可能会遵循相同的示例!;-)

您应该更改行:

instance = new GameObject( "gamestate").AddComponent ();
用于:

instance=新游戏对象(“游戏状态”).AddComponent();
他们的完整代码在此粘贴箱中:

instance = new GameObject( "gamestate").AddComponent ();
instance = new GameObject( "gamestate").AddComponent<gamestate>();