user3525278
user3525278

Reputation: 1

The type arguments for method cannot be inferred from the usage

This is my code and I'm trying to figure out what's the problem with this line..

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

Here is the error that I'm getting: The type arguments for method 'UnityEngine.GameObject.AddComponent<T>()' cannot be inferred from the usage. Try specifying the type arguments explicitly

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");
    }
}

Upvotes: 0

Views: 3578

Answers (2)

orlando2bjr
orlando2bjr

Reputation: 193

I think I might be following the same example! ;-)

You should change the line:

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

for:

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

Their full code is on this pastebin: http://pastebin.com/YPJTGLwX

Upvotes: 1

rutter
rutter

Reputation: 11452

This line:

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

Specifically, this call:

AddComponent ();

What sort of component do you want to add? Let's suppose you want to add an AudioSource:

AddComponent<AudioSource>();

From context, it looks like you want a gamestate:

AddComponent<gamestate>();

For your own reference, these are called generics, which are similar to templates in C++.

As an aside, common practice is to capitalize class names (like Gamestate or GameState). The compiler won't care, but other developers might.

Upvotes: 1

Related Questions