I’ve created a Game Manager (Script) and created a Game Object called (Game Manager)
also, I’ve created a parent Script (Animal) and make 3 Child scripts out of it (lion - dog - cat)
In the Animal script (Parent Script) I’ve created a variable for the game manager and make it public
but didn’t call the game manager in any of the child scripts
However, I didn’t attach the Animal Script to any Object and I attached each child’s script to the asset related to it.
Now when I try to attach the game Manager Object (Hierarchy) to the script attached to the assets (lion - dog - cat) it gives me a blocked sign that cannot attach the object to the script
I called the variable by GameManager…
public class GameManager : MonoBehaviour
{
private int score = 0;
private int lives = 5;
public TextMeshProUGUI scoreText;
public TextMeshProUGUI livesText;
public TextMeshProUGUI gameOverText;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//UpdateScore(0);
//PlayerLives(5);
}
public void UpdateScore(int ScoreToAdd)
{
score += ScoreToAdd;
scoreText.text = ("Score: " + score);
}
public void PlayerLives(int value)
{
lives -= value;
if(lives <= 0)
{
Debug.Log("Game Over");
}
}
public class Animal : MonoBehaviour
{
public GameManager gameManager;
private int m_ScoreValue;
private float m_Speed;
// Start is called before the first frame update
void Start()
{
gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
}
// Update is called once per frame
void Update()
{
Speed();
ScoreToAdd(m_ScoreValue);
}
public virtual void Speed()
{
float speed = m_Speed;
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
public virtual void ScoreToAdd(int ScoreValue)
{
m_ScoreValue = ScoreValue;
gameManager.UpdateScore(m_ScoreValue);
}
public class Stag : Animal
{
private int m_Score = 10;
private float m_Speed = 6;
void Start()
{
}
void Update()
{
ScoreToAdd(m_Score);
Speed();
}
public override void ScoreToAdd(int ScoreValue)
{
base.ScoreToAdd(ScoreValue);
ScoreValue = m_Score;
}
public override void Speed()
{
base.Speed();
float speed = m_Speed;
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
Thanks to everyone who will take his time to help me
