Game Manager & Game Objects to be assgined in Inspector!!

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

Prefabs can’t save references to scene objects because there’s no guarantee that object exists in the same scene that you instantiate the prefab in. You’ll need to fetch it on Start() or re-think your structure.

EDIT: It looks like you’re already finding the GameManager in Start(), so what’s the actual problem? Why do you need to drag the reference at all?

1 Like

Your GameManager is in the scene, while your animals are prefabs.
You cannot assign an object in scene to a prefab in assets. The scene is like a frame of reference, while the assets folder is another. If 2 objects aren’t in the same frame of reference, they cannot interact with each other. Same goes for objects in different scenes.

Since your animals are prefabs, you must intend to spawn them into the scene somehow. Assign the GameManager to the spawned animal when it is instantiated.

2 Likes

I intend to make 3 different types of animals so I’ve created a parent script and 3 child scripts out of it

The score of each animal will be different from the other one and the game manager will be the reference for the score that will be counted

if you have another idea to help me or structure the game in another way;
it will be highly appreciated.