Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Unity 2D Space Shooter

Discussion in '2D' started by Pavlinh, Feb 18, 2021.

  1. Pavlinh

    Pavlinh

    Joined:
    Feb 10, 2021
    Posts:
    4
    I am currently making a 2D Space Shooter game. I encountered an error: Assets\Scripts\PlayerControl.cs(98,89): error CS1061: 'GameObject' does not contain a definition for 'GameManagerState' and no accessible extension method 'GameManagerState' accepting a first argument of type 'GameObject' could be found (are you missing a using directive or an assembly reference?)

    Please help is needed :/

    PlayerControl script:
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine.UI;
    3. using UnityEngine;
    4.  
    5. public class PlayerControl : MonoBehaviour
    6. {
    7.     public GameObject GameManager;
    8.  
    9.     public GameObject PlayerBullet;
    10.     public GameObject BulletPosition01;
    11.     public GameObject BulletPosition02;
    12.     public GameObject Explosion;
    13.  
    14.     public Text LivesUIText;
    15.  
    16.     const int MaxLives = 3; //max lives
    17.     int lives; //current lives
    18.  
    19.     public float speed;
    20.  
    21.     public void Init()
    22.     {
    23.         lives = MaxLives;
    24.  
    25.         //updates UI text lives
    26.         LivesUIText.text = lives.ToString();
    27.  
    28.         //sets this active
    29.         gameObject.SetActive(true);
    30.     }
    31.  
    32.     void Start()
    33.     {
    34.  
    35.     }
    36.  
    37.     void Update()
    38.     {
    39.         //fire bullets (spacebar)
    40.         if (Input.GetKeyDown("space"))
    41.         {
    42.             //instantiate the first bullet
    43.             GameObject Bullet01 = (GameObject)Instantiate(PlayerBullet);
    44.             Bullet01.transform.position = BulletPosition01.transform.position; //set the bullet initial position
    45.  
    46.             //instantiate the second bullet
    47.             GameObject Bullet02 = (GameObject)Instantiate(PlayerBullet);
    48.             Bullet02.transform.position = BulletPosition02.transform.position; //
    49.         }
    50.  
    51.  
    52.         float x = Input.GetAxisRaw("Horizontal"); // left, right
    53.         float y = Input.GetAxisRaw("Vertical"); //up, down
    54.         //direction Vector input
    55.         Vector2 direction = new Vector2(x, y).normalized;
    56.  
    57.         Move(direction);
    58.     }
    59.  
    60.     void Move(Vector2 direction)
    61.     {
    62.         //screen limits player's movement
    63.         Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2(0, 0)); // bottom-left point corner
    64.         Vector2 max = Camera.main.ViewportToWorldPoint(new Vector2(1, 1)); // top-right point corner
    65.  
    66.         max.x = max.x - 0.225f; // takes in to account player sprite width
    67.         min.x = min.x + 0.225f; // add the player sprite half width
    68.  
    69.         max.y = max.y - 0.285f; //takes in to account player sprite height
    70.         min.y = min.y - 0.285f; // add the player sprite half height
    71.  
    72.         // Get the player's current position
    73.         Vector2 pos = transform.position;
    74.  
    75.         // calculate the new position
    76.         pos += direction * speed * Time.deltaTime;
    77.  
    78.         // Make sure the new position is not outside the screen
    79.         pos.x = Mathf.Clamp(pos.x, min.x, max.x);
    80.         pos.y = Mathf.Clamp(pos.y, min.y, max.y);
    81.  
    82.         // Updates player's position
    83.         transform.position = pos;
    84.     }
    85.  
    86.     void OnTriggerEnter2D(Collider2D col)
    87.     {
    88.         //detect collision
    89.         if ((col.tag == "EnemyShipTag") || (col.tag == "EnemyBulletTag"))
    90.         {
    91.             PlayExplosion();
    92.  
    93.             lives--;
    94.             LivesUIText.text = lives.ToString(); //updates UI text lives
    95.  
    96.             if (lives == 0)//player will be dead
    97.             {
    98.                 GameManager.GetComponent<GameManager>().SetGameManagerState(GameManager.GameManagerState.GameOver);
    99.  
    100.                 gameObject.SetActive(false);
    101.             }
    102.         }
    103.     }
    104.     //instantiate the explosion
    105.     void PlayExplosion()
    106.     {
    107.         GameObject explosion = (GameObject)Instantiate(Explosion);
    108.  
    109.         //explosion position
    110.         explosion.transform.position = transform.position;
    111.     }
    112. }
    GameManager script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameManager : MonoBehaviour
    6. {
    7.     public GameObject startButton;
    8.     public GameObject playerShip;
    9.  
    10.     public enum GameManagerState
    11.     {
    12.         Opening,
    13.         Gameplay,
    14.         GameOver,
    15.     }
    16.  
    17.     GameManagerState GMState;
    18.  
    19.     void Start()
    20.     {
    21.         GMState = GameManagerState.Opening;
    22.     }
    23.  
    24.     void UpdateGameManagerState()
    25.     {
    26.         switch(GMState)
    27.         {
    28.             case GameManagerState.Opening:
    29.  
    30.                 break;
    31.             case GameManagerState.Gameplay:
    32.  
    33.                 break;
    34.  
    35.             case GameManagerState.GameOver:
    36.  
    37.                 break;
    38.         }
    39.     }
    40.  
    41.     public void SetGameManagerState(GameManagerState state)
    42.     {
    43.         GMState = state;
    44.         UpdateGameManagerState ();
    45.     }
    46. }
    47.  
     
  2. Pavlinh

    Pavlinh

    Joined:
    Feb 10, 2021
    Posts:
    4
    By the way, I pretty much entirely follow this tutorial:

    I have the script written the same way as in the tutorial ( or so I think), but in the tutorial, there are not any errors.
     
    Last edited: Feb 18, 2021
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,541
    Hi,

    Maybe you already solved this, but here it goes...

    It isn't usually a good idea to name your variables like this:

    Code (CSharp):
    1. public GameObject GameManager;

    You have a variable with a name "GameManager" which is exactly the same as your class name for GameManager.

    Then you are doing this:

    Code (CSharp):
    1. GameManager.GetComponent<GameManager>().SetGameManagerState(GameManager.GameManagerState.GameOver);

    So, your GameManager variable doesn't have this enum, but your class does. Easiest fix is to use a different variable name:

    Code (CSharp):
    1. public GameObject gameManager; // no capital letter in variable name

    Then your code will work with these changes:

    Code (CSharp):
    1. gameManager.GetComponent<GameManager>().SetGameManagerState(GameManager.GameManagerState.GameOver);

    First you get the component from a gameobject stored in your variable called "gameManager" that is of type GameObject. Then you get your GameManager script with GetComponent. Now you can also access the GameManagerState enum (which you had nested inside your GameManager class) from your class GameManager with
    GameManager.GameManagerState.GameOver
    .

    Note. You could type your variable as GameManager to begin with, so you don't have to do that GetComponent:
    Code (CSharp):
    1. // Your variable:
    2. public GameManager gameManager;
    3.  
    4. // later in your code, no get component
    5. gameManager.SetGameManagerState(GameManager.GameManagerState.GameOver);
    6.  
    I think (I've never used these so maybe someone can correct me if I'm wrong) you could also define a type alias to avoid this conflict, but I don't think that is a good idea (simply use a better name for your variable or move the enum outside of GameManager class):
    Code (CSharp):
    1. // In the beginning of your code:
    2. using UnityEngine.UI;
    3. using UnityEngine;
    4. using MyGameManager = GameManager;
    5.  
    6. // in your code:
    7. GameManager.GetComponent<GameManager>().SetGameManagerState(MyGameManager.GameManagerState.GameOver);
     
    Last edited: Feb 20, 2021
unityunity