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

Script not checking if a different script is active.

Discussion in 'Scripting' started by Code1345, Jun 13, 2018.

  1. Code1345

    Code1345

    Joined:
    May 21, 2017
    Posts:
    61
    Hello! So I have run into an issue where I need one of my scripts to check if the other is active, so it knows that if it isn't active, the player shouldn't be able to take damage from the enemy. Right now though, the script doesn't seem able to tell if it's active or not, since I have a debug.log that's not running. Thank you in advance for any responses!
    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 HealthScript : MonoBehaviour {
    8.     public Text healthcounter;
    9.     public int health;
    10.     public Collision enemysword;
    11.     public Image healthbar;
    12.     public bool blocked;
    13.     private MovingScript movingscript;
    14.     public GameObject knight;
    15.     public GameObject player;
    16.       // Use this for initialization
    17.     void Start () {
    18.         health = 100;
    19.         movingscript = knight.GetComponent<MovingScript>();
    20.         player = GameObject.FindGameObjectWithTag("Player");
    21.         knight = GameObject.FindGameObjectWithTag("Enemy");
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update () {
    26.         if (Input.GetMouseButtonDown(1))
    27.         {
    28.             blocked = true;
    29.          
    30.         }
    31.  
    32.             healthcounter.text = "Health = " + health;      
    33.  
    34.         if (health <= 0)
    35.         {
    36.             healthbar.transform.localScale = new Vector3(0, 0, 0);
    37.             health = 0;
    38.         }
    39.         if(health == 0)
    40.         {
    41.             Destroy(player);
    42.             SceneManager.LoadScene("GameOver", LoadSceneMode.Single);
    43.         }
    44.         if(movingscript.enabled == false)
    45.         {
    46.             Debug.Log("working");
    47.         }
    48.      
    49.  
    50.  
    51.     }
    52.     private void OnTriggerEnter(Collider other)
    53.     {
    54.         if (other.gameObject.tag == "Enemy" && blocked == false && movingscript.enabled == true)
    55.         {
    56.            
    57.             health -= 10;
    58.             healthcounter.text = "Health = " + health;
    59.             healthbar.transform.localScale -= new Vector3(0.15f, 0, 0);
    60.          
    61.  
    62.  
    63.  
    64.         }
    65.         blocked = false;
    66.     }
    67.  
    68. }
    69.  
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    If that Debug.Log() isn't running [and there are no red errors in the console] then the script is either disabled, it's parent object is disabled or the script is not even in the scene.
     
  3. Code1345

    Code1345

    Joined:
    May 21, 2017
    Posts:
    61
    I would think so as well, but the rest of the script runs fine.
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    Maybe you have Log messages filtered on the console.
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    My guess is you're ignoring the null reference error you should be hitting on line 19, since you're running GetComponent on an object who's reference you don't assign until line 21. You're probably also getting a null reference error on line 44 is my guess as well. You should address that issue first, since a null reference error can cause any following code to not be run.

    I guess you might not be getting the first null reference error if you are doing something weird where you assign the reference in the inspector, and then reassign it in start. Not sure what you're doing with that. Why are you doing GetComponent on line 19, and then doing a Find for the object who's component you've already got on line 21, and then just keep using the component from line 19 on line 44? I don't get it.
     
  6. Code1345

    Code1345

    Joined:
    May 21, 2017
    Posts:
    61
    Yep, I noticed that a little while ago and fixed it- That didn't solve the issue, but I ended up figuring it out! Turns out I was making stuff way more complicated than it needed to be, and just substituted in a bool for some stuff. I appreciate the help!