Complete beginner in Unity and have faced the issue. I cannot drag the asset Player from left side to the inspector panel player field while the others work fine like Game Over and start button. Any help is appreciated.
If you can’t drag-and-drop it into the field it means that it doesn’t have the correct component on it.
Can you show your GameManager script? Make sure your player variable looks like this:
[SerializeField] private GameObject player;
GameManager script:
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public static GameManager Instance { get; private set; }
public Player player;
public Text scoreText;
public GameObject playButton;
public GameObject gameOver;
private int score;
private void Awake()
{
if (Instance != null)
{
DestroyImmediate(gameObject);
}
else
{
Instance = this;
Application.targetFrameRate = 60;
DontDestroyOnLoad(gameObject);
Pause();
}
}
public void Play()
{
score = 0;
scoreText.text = score.ToString();
playButton.SetActive(false);
gameOver.SetActive(false);
Time.timeScale = 1f;
player.enabled = true;
Pipes[ ] pipes = FindObjectsOfType();
for (int i = 0; i < pipes.Length; i++) {
Destroy(pipes*.gameObject);*
}
}
public void GameOver()
{
playButton.SetActive(true);
gameOver.SetActive(true);
Pause();
}
public void Pause()
{
Time.timeScale = 0f;
player.enabled = false;
}
public void IncreaseScore()
{
score++;
scoreText.text = score.ToString();
}
}
using UnityEngine;
using UnityEngine.UI;
[DefaultExecutionOrder(-1)]
public class GameManager : MonoBehaviour
{
public static GameManager Instance { get; private set; }
[SerializeField] private Player player;
[SerializeField] private Spawner spawner;
[SerializeField] private Text scoreText;
[SerializeField] private GameObject playButton;
[SerializeField] private GameObject gameOver;
private int score;
public int Score => score;
private void Awake()
{
if (Instance != null)
{
DestroyImmediate(gameObject);
}
else
{
Instance = this;
Application.targetFrameRate = 60;
DontDestroyOnLoad(gameObject);
Pause();
}
}
public void Play()
{
score = 0;
scoreText.text = score.ToString();
playButton.SetActive(false);
gameOver.SetActive(false);
Time.timeScale = 1f;
player.enabled = true;
Pipes[ ] pipes = FindObjectsOfType();
for (int i = 0; i < pipes.Length; i++) {
Destroy(pipes*.gameObject);*
}
}
public void GameOver()
{
playButton.SetActive(true);
gameOver.SetActive(true);
Pause();
}
public void Pause()
{
Time.timeScale = 0f;
player.enabled = false;
}
public void IncreaseScore()
{
score++;
scoreText.text = score.ToString();
}
}
I assume that “Player” is a script that is supposed to control your player GameObject. Check that you have attached the Player script to your player in the first place. If the player does not have a Player script attached, you cannot use it in a Player script variable.
Thanks for the answer. I checked and the player script is attached to GameObject Player. I changed the snippet to this but still doesn’t work:
using UnityEngine;
using UnityEngine.UI;
[DefaultExecutionOrder(-1)]
public class GameManager : MonoBehaviour
{
public static GameManager Instance { get; private set; }
[SerializeField] private Player player;
[SerializeField] private Spawner spawner;
[SerializeField] private Text scoreText;
[SerializeField] private GameObject playButton;
[SerializeField] private GameObject gameOver;
private int score;
public int Score => score;
private void Awake()
{
if (Instance != null)
{
DestroyImmediate(gameObject);
}
else
{
Instance = this;
Application.targetFrameRate = 60;
DontDestroyOnLoad(gameObject);
Pause();
}
}
public void Play()
{
score = 0;
scoreText.text = score.ToString();
playButton.SetActive(false);
gameOver.SetActive(false);
Time.timeScale = 1f;
player.enabled = true;
Pipes[ ] pipes = FindObjectsOfType();
for (int i = 0; i < pipes.Length; i++) {
Destroy(pipes*.gameObject);*
}
}
public void GameOver()
{
playButton.SetActive(true);
gameOver.SetActive(true);
Pause();
}
public void Pause()
{
Time.timeScale = 0f;
player.enabled = false;
}
public void IncreaseScore()
{
score++;
scoreText.text = score.ToString();
}
}
The error message:
You have a compile error in your code. Code that doesn’t compile properly won’t attach properly either. Show us the Player
class. Also use code tags. It’s almost impossible to read code pasted like that.
This is Player script:
***********************************
using UnityEngine;
public class birdscript : MonoBehaviour
{
public Sprite[] sprites;
public float strength = 5f;
public float gravity = -9.81f;
public float tilt = 5f;
private SpriteRenderer spriteRenderer;
private Vector3 direction;
private int spriteIndex;
private void Awake()
{
spriteRenderer = GetComponent<SpriteRenderer>();
}
private void Start()
{
InvokeRepeating(nameof(AnimateSprite), 0.15f, 0.15f);
}
private void OnEnable()
{
Vector3 position = transform.position;
position.y = 0f;
transform.position = position;
direction = Vector3.zero;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) {
direction = Vector3.up * strength;
}
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began) {
direction = Vector3.up * strength;
}
}
// Apply gravity and update the position
direction.y += gravity * Time.deltaTime;
transform.position += direction * Time.deltaTime;
// Tilt the bird based on the direction
Vector3 rotation = transform.eulerAngles;
rotation.z = direction.y * tilt;
transform.eulerAngles = rotation;
}
private void AnimateSprite()
{
spriteIndex++;
if (spriteIndex >= sprites.Length) {
spriteIndex = 0;
}
if (spriteIndex < sprites.Length && spriteIndex >= 0) {
spriteRenderer.sprite = sprites[spriteIndex];
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Obstacle")) {
GameManager.Instance.GameOver();
} else if (other.gameObject.CompareTag("Scoring")) {
GameManager.Instance.IncreaseScore();
}
}
}
This is GameManager script:
****************************
using UnityEngine;
using UnityEngine.UI;
[DefaultExecutionOrder(-1)]
public class GameManager : MonoBehaviour
{
public static GameManager Instance { get; private set; }
[SerializeField] private Player player;
[SerializeField] private Spawner spawner;
[SerializeField] private Text scoreText;
[SerializeField] private GameObject playButton;
[SerializeField] private GameObject gameOver;
private int score;
public int Score => score;
private void Awake()
{
if (Instance != null)
{
DestroyImmediate(gameObject);
}
else
{
Instance = this;
Application.targetFrameRate = 60;
DontDestroyOnLoad(gameObject);
Pause();
}
}
public void Play()
{
score = 0;
scoreText.text = score.ToString();
playButton.SetActive(false);
gameOver.SetActive(false);
Time.timeScale = 1f;
player.enabled = true;
Pipes[] pipes = FindObjectsOfType<Pipes>();
for (int i = 0; i < pipes.Length; i++) {
Destroy(pipes[i].gameObject);
}
}
public void GameOver()
{
playButton.SetActive(true);
gameOver.SetActive(true);
Pause();
}
public void Pause()
{
Time.timeScale = 0f;
player.enabled = false;
}
public void IncreaseScore()
{
score++;
scoreText.text = score.ToString();
}
}
I don’t know how but the issue of not being able to drag-n-drop Player script is fixed now but the game is not playing. Now I get the message as attached
After several restart of unity it’s working now! Thanks for the help but don’t know how it worked!