- Home /
How do I access a bool from another script?
Hey! I know this has been discussed in other posts but it's not working for me even after doing the same as other people. So I have a bool in one script called bobScript that is in a gameobject called Bob.
public bool isCounting;
void Start () {
isCounting = true;
}
I want to access that bool in another script called Timer that is in an empty gameobject called TimerScript. What I did was this:
if (GameObject.Find("Bob").GetComponent<bobScript>().isCounting) {
//code
}
What I want to do is, when the bool is true, a timer is showing the time, and when it's false it stops. But when I press play the timer doesn't even start. It's like the bool is false while it isn't.
Please help. Thanks!
I really don't recommend you to use GameObject.Find and better use a direct link to the gameobject (add a property public GameObject bob;
) and use bob.GetComponent<bobScript>()
or direct link to the script (add a property public bobScript BobScript;
) and access the script like this: if (BobScript.isCounting) { }
Be sure bobScript is placed on a gameobject as well. Dumb to say, because I told you to directly attach the object with that script and in order for it to uh.. exist and get the component, it needs to be somewhere first.
Answer by goutham12 · Oct 10, 2017 at 12:51 PM
//class A
public class bobScript: MonoBehaviour {
public bool isCounting;
void Start () {
isCounting = true;
}
}
//class B public class ClassB: MonoBehaviour {
public bobScript bS;
void Update()
{
if(bS.isCounting ){
//do
}
}
}
now drag and drop your object(which is having bobScript ) to the object (which is having classB)
Thank you so much for this!
I had a problem where my bool in Class A was protected in Class B. The problem was that I forgot to make the bool in Class A a public bool.
When my character health reached zero, I wanted him to be dead. If he was dead in my damage script, I wanted the points to stop increasing over time in my score script.
Answer by jeango · Oct 10, 2017 at 01:12 PM
First of all, using GameObject.Find is not really good practice. You should, whenever possible, have a direct reference to bob.
Your code makes assumptions about what bob is made of:
There's an object on the scene named "Bob"
That object has a component of type "bobScript"
And in your "if" statement you're not even checking if these assumptions are actually correct.
Also, you don't tell us where in your code you're actually making that test. For all we know it is even possible that bob's "Start" method was not even invoked yet when you check that bool in your other object.
So first, like goutham12 suggests, let's start with good practices, and have a reference in your other object:
public bobScript bob;
now you can drag and drop the bob on your scene directly to that variable in the inspector (provided it indeed contains a bobScript component).
The second part (and that's what I suspect your problem is) is to make sure you check your boolean when we know for sure that bob has indeed started.
I suspect you're either making this check in Awake or in OnEnable (both of which are guaranteed to be called BEFORE Start is called on bob). But it's also possible that you do this in Start. In that case, ask yourself what makes the most sense: do you want bob to start counting when it awakes or when it starts? I would tend to either set this boolean in Awake, or in OnEnable, especially if you're going to check that bool in other objects' start method
I would like to know your opinion on FindObjectOfType<ExampleClass>()
, do you think it is a good practice to assign a class to a variable and then use it like so ExampleClass.aFunction()
?
Is there a simpler, maybe a cleaner way of doing the same thing or is this the best way?
Avoid using FindObjectOfType if you can. What it does is it goes through all the hierarchy of game objects, one by one and finds the components of requested type. This will be quite resource hungry and it is very slow.
$$anonymous$$ore info at : https://docs.unity3d.com/ScriptReference/Object.FindObjectOfType.html
I think this is the better answer.
You are right that GameObject.Find is not good practise, it couples the code into a mere string definition, you could get around it by strongly typing out the names in a static class first. If you event wanted to change the code you would have to change the single strings everywhere which is very bad practise. Drag and dropping I think is a Good solution but it's not the best. I've had issues before where Unity3D would lose all the references, so all drag and dropped scripts would dissapear obviously from it's game objects.
Two best solutions is to use dependency injection, via constructor. There are DI frameworks availabile for unity3d e.g. like zenject. OR an Entity component system.
Your answer
Follow this Question
Related Questions
Stop Player movement when bool changes 2D 3 Answers
Multiple Cars not working 1 Answer
Keep boolean true whilst playing animation, then false 1 Answer
Distribute terrain in zones 3 Answers
2D platformer- getting errors I don't understand (c#) 1 Answer