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): using System.Collections; using UnityEngine.UI; using UnityEngine; public class PlayerControl : MonoBehaviour { public GameObject GameManager; public GameObject PlayerBullet; public GameObject BulletPosition01; public GameObject BulletPosition02; public GameObject Explosion; public Text LivesUIText; const int MaxLives = 3; //max lives int lives; //current lives public float speed; public void Init() { lives = MaxLives; //updates UI text lives LivesUIText.text = lives.ToString(); //sets this active gameObject.SetActive(true); } void Start() { } void Update() { //fire bullets (spacebar) if (Input.GetKeyDown("space")) { //instantiate the first bullet GameObject Bullet01 = (GameObject)Instantiate(PlayerBullet); Bullet01.transform.position = BulletPosition01.transform.position; //set the bullet initial position //instantiate the second bullet GameObject Bullet02 = (GameObject)Instantiate(PlayerBullet); Bullet02.transform.position = BulletPosition02.transform.position; // } float x = Input.GetAxisRaw("Horizontal"); // left, right float y = Input.GetAxisRaw("Vertical"); //up, down //direction Vector input Vector2 direction = new Vector2(x, y).normalized; Move(direction); } void Move(Vector2 direction) { //screen limits player's movement Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2(0, 0)); // bottom-left point corner Vector2 max = Camera.main.ViewportToWorldPoint(new Vector2(1, 1)); // top-right point corner max.x = max.x - 0.225f; // takes in to account player sprite width min.x = min.x + 0.225f; // add the player sprite half width max.y = max.y - 0.285f; //takes in to account player sprite height min.y = min.y - 0.285f; // add the player sprite half height // Get the player's current position Vector2 pos = transform.position; // calculate the new position pos += direction * speed * Time.deltaTime; // Make sure the new position is not outside the screen pos.x = Mathf.Clamp(pos.x, min.x, max.x); pos.y = Mathf.Clamp(pos.y, min.y, max.y); // Updates player's position transform.position = pos; } void OnTriggerEnter2D(Collider2D col) { //detect collision if ((col.tag == "EnemyShipTag") || (col.tag == "EnemyBulletTag")) { PlayExplosion(); lives--; LivesUIText.text = lives.ToString(); //updates UI text lives if (lives == 0)//player will be dead { GameManager.GetComponent<GameManager>().SetGameManagerState(GameManager.GameManagerState.GameOver); gameObject.SetActive(false); } } } //instantiate the explosion void PlayExplosion() { GameObject explosion = (GameObject)Instantiate(Explosion); //explosion position explosion.transform.position = transform.position; } } GameManager script: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class GameManager : MonoBehaviour { public GameObject startButton; public GameObject playerShip; public enum GameManagerState { Opening, Gameplay, GameOver, } GameManagerState GMState; void Start() { GMState = GameManagerState.Opening; } void UpdateGameManagerState() { switch(GMState) { case GameManagerState.Opening: break; case GameManagerState.Gameplay: break; case GameManagerState.GameOver: break; } } public void SetGameManagerState(GameManagerState state) { GMState = state; UpdateGameManagerState (); } }
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.
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): 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): 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): public GameObject gameManager; // no capital letter in variable name Then your code will work with these changes: Code (CSharp): 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): // Your variable: public GameManager gameManager; // later in your code, no get component gameManager.SetGameManagerState(GameManager.GameManagerState.GameOver); 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): // In the beginning of your code: using UnityEngine.UI; using UnityEngine; using MyGameManager = GameManager; // in your code: GameManager.GetComponent<GameManager>().SetGameManagerState(MyGameManager.GameManagerState.GameOver);