I wan to use the same variable birdIsAlive in the BirdScript and LogicScript
LogicScript:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LogicScript : MonoBehaviour
{
public int playerScore;
public Text scoreText;
public GameObject gameOverScreen;
public BirdScript bird;
void Start()
{
bird = GameObject.FindGameObjectWithTag("Logic").GetComponent<BirdScript>();
Debug.Log(bird.birdIsAlive);
}
[ContextMenu("Increase Score")]
public void addScore(int scoreToAdd)
{
if (bird.birdIsAlive == true)
{
playerScore += scoreToAdd;
scoreText.text = playerScore.ToString();
}
}
public void restartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
public void gameOver()
{
gameOverScreen.SetActive(true);
}
}
BirdScript:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BirdScript : MonoBehaviour
{
public Rigidbody2D myRigidbody;
public float flapStrength;
public LogicScript logic;
public bool birdIsAlive = true;
// Start is called before the first frame update
void Start()
{
logic = GameObject.FindGameObjectWithTag("Logic").GetComponent<LogicScript>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) == true && birdIsAlive == true) //dont need == true, mean the same thing
{
myRigidbody.velocity = Vector2.up * flapStrength;
}
if (transform.position.y < -14)
{
logic.gameOver();
birdIsAlive = false;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
logic.gameOver();
birdIsAlive = false;
}
}