Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question Uses same variable in different scripts

Discussion in 'Scripting' started by johnnysuriano, May 31, 2024.

  1. johnnysuriano

    johnnysuriano

    Joined:
    May 13, 2024
    Posts:
    14
    I wan to use the same variable birdIsAlive in the BirdScript and LogicScript

    LogicScript:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class LogicScript : MonoBehaviour
    8. {
    9.     public int playerScore;
    10.     public Text scoreText;
    11.     public GameObject gameOverScreen;
    12.     public BirdScript bird;
    13.  
    14.     void Start()
    15.     {
    16.         bird = GameObject.FindGameObjectWithTag("Logic").GetComponent<BirdScript>();
    17.         Debug.Log(bird.birdIsAlive);
    18.     }
    19.  
    20.     [ContextMenu("Increase Score")]
    21.     public void addScore(int scoreToAdd)
    22.     {
    23.         if (bird.birdIsAlive == true)
    24.         {
    25.             playerScore += scoreToAdd;
    26.             scoreText.text = playerScore.ToString();
    27.         }
    28.     }
    29.  
    30.     public void restartGame()
    31.     {
    32.         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    33.     }
    34.  
    35.     public void gameOver()
    36.     {
    37.         gameOverScreen.SetActive(true);
    38.     }
    39. }
    BirdScript:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BirdScript : MonoBehaviour
    6. {
    7.     public Rigidbody2D myRigidbody;
    8.     public float flapStrength;
    9.     public LogicScript logic;
    10.     public bool birdIsAlive = true;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.        logic = GameObject.FindGameObjectWithTag("Logic").GetComponent<LogicScript>();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         if (Input.GetKeyDown(KeyCode.Space) == true && birdIsAlive == true) //dont need == true, mean the same thing
    22.         {
    23.             myRigidbody.velocity = Vector2.up * flapStrength;
    24.         }
    25.         if (transform.position.y < -14)
    26.         {
    27.             logic.gameOver();
    28.             birdIsAlive = false;
    29.         }
    30.     }
    31.  
    32.     private void OnCollisionEnter2D(Collision2D collision)
    33.     {
    34.         logic.gameOver();
    35.         birdIsAlive = false;
    36.     }
    37. }
    38.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,571
    Referencing GameObjects, Scripts, variables, fields, methods (anything non-static) in other script instances or GameObjects:

    https://forum.unity.com/threads/hel...-vars-in-another-script.1076825/#post-6944639

    https://forum.unity.com/threads/accessing-a-gameobject-in-different-scene.1103239/

    It isn't always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.

    Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.

    That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.