CS0411 Unity error

Asked

Viewed 30 times

0

I have this error in my code that I am not able to solve. the error code: error CS0411: The type arguments for method 'GameObject.GetComponent<T>()' cannot be inferred from the usage. Try specifying the type arguments explicitly.

The function I’m having error

  public void CreateAsteroids(float asteroidsNum)
    {
        for (int i = 1; i <= asteroidsNum; i++)
        {
            GameObject AsteroidClone = Instantiate(asteroid, new Vector2(Random.Range(-10, 10), 6f), transform.rotation);

            AsteroidClone.GetComponent().SetGeneration(1); // Nesta linha
            AsteroidClone.SetActive(true);
        }
    }

My complete code

using UnityEngine;

public class Gameplay : MonoBehaviour
{



   public GameObject asteroid;
    public GameObject rocket;
    private int _startLevelAsteroidsNum;
    private bool _allAsteroidsOffScreen;
    private int levelAsteroidNum;
    private Camera mainCam;
    private int asteroidLife;

    private void Start()
    {
        asteroid.SetActive(false);
        mainCam = Camera.main;
        _startLevelAsteroidsNum = 2;
        CreateAsteroids(_startLevelAsteroidsNum);
    }

    private void Update()
    {
        RenderSettings.skybox.SetFloat("_Rotation", Time.time * 0.8f);

        if (asteroidLife <= 0)
        {
            asteroidLife = 6;
            CreateAsteroids(1);
        }

        float sceneWidth = mainCam.orthographicSize * 2 * mainCam.aspect;
        float sceneHeight = mainCam.orthographicSize * 2;
        float sceneRightEdge = sceneWidth / 2;
        float sceneLeftEdge = sceneRightEdge * -1;
        float sceneTopEdge = sceneHeight / 2;
        float sceneBottomEdge = sceneTopEdge * -1;

        _allAsteroidsOffScreen = true;

    }

    public void CreateAsteroids(float asteroidsNum)
    {
        for (int i = 1; i <= asteroidsNum; i++)
        {
            GameObject AsteroidClone = Instantiate(asteroid, new Vector2(Random.Range(-10, 10), 6f), transform.rotation);

            AsteroidClone.GetComponent().SetGeneration(1);
            AsteroidClone.SetActive(true);
        }
    }

    public void RocketFail()
    {
        Cursor.visible = true;
        print("GAME OVER");
    }

    public void asterodDestroyed()
    {
        asteroidLife--;
    }

    public int startLevelAsteroidsNum
    {
        get { return _startLevelAsteroidsNum; }
    }

    public bool allAsteroidsOffScreen
    {
        get { return _allAsteroidsOffScreen; }
    }

}
  • https://answall.com/questions/440045/unity-c-error-cs0411#comment849408_440045

  • That didn’t solve my problem, unfortunately...

1 answer

0

Friend the Getcomponent method needs to receive a generic type (abstract class), for example if it is the type class Asteroid, it would look like this:

public void CreateAsteroids(float asteroidsNum)
    {
        for (int i = 1; i <= asteroidsNum; i++)
        {
            GameObject AsteroidClone = Instantiate(asteroid, new Vector2(Random.Range(-10, 10), 6f), transform.rotation);

            AsteroidClone.GetComponent<Asteroid>().SetGeneration(1); // Nesta linha
            AsteroidClone.SetActive(true);
        }
    }

Among the keys is the abstract type of data, otherwise it will not know what it will receive and which method you can use. This goes into polymorphism, generic type, I recommend after you take a look, because in strongly typed language, it is very interesting to have notions about it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.