Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

If statement for a bool value in other script

Discussion in 'Scripting' started by HarryWhitelegg02, Mar 19, 2020.

  1. HarryWhitelegg02

    HarryWhitelegg02

    Joined:
    Mar 11, 2020
    Posts:
    39
    I want to be able to have an if() statement in my script which checks whether a bool value is true or false then do something, but I had many issues with it.

    This code is attached to the "player"
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class NextLevel : MonoBehaviour
    7. {
    8.  
    9.     public Transform playerPrefab;
    10.     public Transform spawnPointlvl2;
    11.     public GameObject Level_1;
    12.     public bool Level1;
    13.  
    14.     void Start()
    15.     {
    16.         Level1 = true;
    17.     }
    18.  
    19.     void OnTriggerEnter2D(Collider2D other)
    20.     {
    21.  
    22.         if (other.gameObject.tag == "EndTrig")
    23.         {
    24.         playerPrefab.transform.position = spawnPointlvl2.transform.position;
    25.         Destroy(Level_1);
    26.         Level1 = false;
    27.         }
    28.     }
    29. }
    And this is attached to a separate empty object that is the game manager.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameMaster : MonoBehaviour
    6. {
    7.     public static GameMaster gm;
    8.     public nextlevelScript nL;
    9.     public Transform playerPrefab;
    10.     public Transform spawnPoint;
    11.     public Transform spawnPoint2;
    12.  
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         if (gm == null)
    18.         {
    19.             gm = GameObject.FindGameObjectWithTag("GM").GetComponent<GameMaster>();
    20.         }
    21.        
    22.     }
    23.    
    24.  
    25.  
    26.     public void RespawnPlayer()
    27.     {
    28.         if("check the bool value here")
    29.         {
    30.         Debug.Log("Player Respawn");
    31.         playerPrefab.transform.position = spawnPoint.transform.position;
    32.         }
    33.  
    34.  
    35.  
    36.     }
    37.  
    38. }
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    So the bool value you want to check is exactly where? In your player? Or NextLevel?

    These are not good programming practices but as a simple example.
    To get a script, like Player in in a GameObject called Player:
    Code (CSharp):
    1. var player = GameObject.Find("Player").GetComponent<Player>();
    Then you need to have a publicly accessible field or a property in that object, so:
    Code (CSharp):
    1. public bool someBoolean;
    or
    Code (CSharp):
    1. bool _someBoolean;
    2. public bool someBoolean
    3. {
    4.     get { return _someBoolean; }
    5.     set { _someBoolean = value; }
    6. }
    And to check the value in that other component you accessed from that player:
    Code (CSharp):
    1. if (player.someBoolean == true)
    2. {
    3.     // Then do something
    4. }
    So, in your case, if the object you wanted to access is an instance of that NextLevel, you would just get that NextLevel component instead of my example Player.

    EDIT: if you need more help, just ask. I had to do some edits on this as I was busy here...
     
    Last edited: Mar 19, 2020