- Home /
How do I check a bool from another script??
Hi so I've been looking through forms all night.. and haven't found something that will work.. In EnemyScript i am trying to see if a bool from PlayerScript is false:
EnemyScript: using UnityEngine; using System.Collections;
public class EnemyScript : MonoBehaviour { public PlayerScript checkDeath;
void Start ()
{
GameObject g = GameObject.FindGameObjectWithTag ("Player");
checkDeath = g.GetComponent<PlayerScript> ();
}
void OnMouseDown ()
{
if (health <=0)
//if the health is 0 or less..
{
if(checkDeath.Dead = false)
//and if the object isn't active..
{
Destroy(gameObject);
}
}
}
I took out the unimportant stuff.. but i'm trying to find out if the bool "Dead" from PlayerScript is false.. and if it is then to destroy the object..
Im not sure what you need to see from PlayerScript other than the fact that there is a public bool on it.. Please help!! thanks!!
Hello!
You have:
GameObject g = GameObject.FindGameObjectWithTag ("Player");
Which will look for the first GameObject with tag Player.
Thus all your EnemyScrip will use the same reference. Is this intended?
Hello @$$anonymous$$ikilo
I just missed your comment while posting the answer. I'll delete my answer but after one clarification . Is @Rosscoe3 's if condition is not wrong ? You can check my answer and tell me. I don't want to give wrong suggestion to anyone. So please verify. & also I'm assu$$anonymous$$g there is only one player in the whole game.
Answer by itsharshdeep · Sep 01, 2015 at 04:00 AM
From your code what I understand is your check condition is assigning the value instead of checking, you need to place "==" instead of "=" in 'if' condition.Try to replace the code
if(checkDeath.Dead = false)
//and if the object isn't active..
{
Destroy(gameObject);
}
To
if(checkDeath.Dead == false) // or can also use if(!checkDeath.Dead)
//and if the object isn't active..
{
Destroy(gameObject);
}
Or second problem you may facing is the Player is dead and he is not more present in the scene so the script is also get destroyed , but you are trying to access that but it is giving you the error.
Thanks for the comment @itsharshdeep !! hmm.. well it is not working sadly :( The player himself is not dead... but the game is not finding the value of the bool.. not sure why this is happening. heres the error:
NullReferenceException: Object reference not set to an instance of an object EnemyScript.On$$anonymous$$ouseDown () (at Assets/Scripts/Enemy Scripts/EnemyScript.cs:50) UnityEngine.Send$$anonymous$$ouseEvents:DoSend$$anonymous$$ouseEvents(Int32, Int32)
It says checkDeath is null. But you are trying to use it.
That also says:
GameObject g = GameObject.FindGameObjectWithTag ("Player");
checkDeath = g.GetComponent<PlayerScript> ();
does not find GameObject, or it contains no PlayerScript.
Please search in you hierarchy for 'PlayerScript' & check on which component the script is attached even in Play mode. $$anonymous$$indly keep track on that.
Or I you agree I can help you on TeamViewer ($$anonymous$$ay be I can sought out)
Thanks.
Answer by Kajal savani · Sep 02, 2015 at 09:39 AM
Declare your dead bool type to any other script (Public Static bool Dead;) that script is not destroy like PlayerScriptis destroy when Player gameobject is destroyed...
and You can access that dead bool variable to playerScript or any script like(othertScrpt. Dead = true;)....
Seriously... Do not give advice like that... It is like, you dude want an ice cream? Go to the factory, get some stuff, buy a fridge, prepare your meal and come back in 12 hours. Ins$$anonymous$$d of simply buying one at the shop nearby...
Using a static for that kind of problem is neither relevant, nor smart.
At the end of your career of developer, you will only use static and keep such bad behaviours like cockroach.
No no no and no, do not you use static. It is overkill.
Your answer
Follow this Question
Related Questions
Can't Call function in another script 1 Answer
How do I change a clicked on GameObjects variable when a boolean (Next Turn) is true? 1 Answer
Get bool value from one script to another (C#) 2 Answers
GetComponent, set boolean to true but it willn't revert 3 Answers
Has my project's data corrupted? Script keeps returning null but 20 minutes ago it wasn't? 1 Answer