- Home /
Script not checking if a different script is active.
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!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class HealthScript : MonoBehaviour {
public Text healthcounter;
public int health;
public Collision enemysword;
public Image healthbar;
public bool blocked;
private MovingScript movingscript;
public GameObject knight;
public GameObject player;
// Use this for initialization
void Start () {
health = 100;
movingscript = knight.GetComponent<MovingScript>();
player = GameObject.FindGameObjectWithTag("Player");
knight = GameObject.FindGameObjectWithTag("Enemy");
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(1))
{
blocked = true;
}
healthcounter.text = "Health = " + health;
if (health <= 0)
{
healthbar.transform.localScale = new Vector3(0, 0, 0);
health = 0;
}
if(health == 0)
{
Destroy(player);
SceneManager.LoadScene("GameOver", LoadSceneMode.Single);
}
if(movingscript.enabled == false)
{
Debug.Log("working");
}
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Enemy" && blocked == false && movingscript.enabled == true)
{
health -= 10;
healthcounter.text = "Health = " + health;
healthbar.transform.localScale -= new Vector3(0.15f, 0, 0);
}
blocked = false;
}
}
And what issue are you facing? The code seems fine (but could be clearly optimized)
The player continues to take damage, even though the script is inactive- and the Debug.Log isn’t running at all, so I don’t think this script is able to tell that the other script is active, if that makes any sense hah. And it definitely needs to be optimized, you’re 100% correct on that.
Are you sure that the $$anonymous$$ovingScript
is on the knight itself not one of it's children?
Answer by tormentoarmagedoom · Jun 13, 2018 at 04:04 PM
Good day.
Look at this: you are defining the this variable sin this order:
health = 100;
movingscript = knight.GetComponent<MovingScript>();
player = GameObject.FindGameObjectWithTag("Player");
knight = GameObject.FindGameObjectWithTag("Enemy");
How moving script will know what is "knight" if is still not defined?
Bye!
Thank you for your response! That didn't solve the problem, but I appreciate the response anyways. I ended up realizing I was over complicating it, and just substituted some bools in. Thank you again!
Your answer
Follow this Question
Related Questions
Guys i really need your help! Why i cant use unity addons? 0 Answers
How can you make an object visible in Unity when you jump on it? 0 Answers
Why does a script launched in Visual Studio from Unity say "Unrecognized Guid Format"? 1 Answer
How to destroy multiple box colliders through C# script 1 Answer
How to get referenced object from TransparentProxy? 0 Answers