I am new to unity and I am trying to make a flappy bird as a project.
But I am stuck in one place.
I created a game control script and added game over text and it worked fine. But when I tried to add to score text to a different script, it didn't work.
It's hard to explain in words, here's a pic and script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameControl : MonoBehaviour
{
public static GameControl instance;
public GameObject gameOverText;
public Text scoreText;
public bool gameOver = false;
public float scrollingSpeed = -1.5f;
private int score = 0;
void Awake()
{
if(instance == null)
{
instance = this;
}
else if(instance != this)
{
Destroy(gameObject);
}
}
void Update()
{
if(gameOver == true && Input.GetMouseButtonDown(0))
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
public void PlayerScored()
{
if(gameOver)
{
return;
}
score ;
scoreText.text = "Score: " score.ToString();
}
public void PlayerDied()
{
gameOverText.SetActive(true);
gameOver = true;
}
}
CodePudding user response:
The gameobject you are trying to drag into the Text
slot doesn't have a "Text" component which looks like this:
Add this component to your gameobject.
If the gameobject already has the "Text" component, there must be some other problem which is that you don't have "Event System" in your scene which probably got deleted accidentally. Right click on your hierarchy and add "Event System".