Changing bools in other GameObject scripts with if statements

Hello,

Thanks in advance for your help. I’m very new to this but one has to start somewhere. I’m trying to write two C# scripts on two separate game objects that will let me toggle a bool via an IF statement in one of the scripts. I’ve read a lot of posts about GetComponent and GameObject.Find and it all seems pretty intuitive. However when I try and write my own code Unity throws me errors. I’m sure I’m just breaking a syntax rule (or several) but as a rookie It’s eluding me. My code is bellow.

This is the first script which holds the bool I want to flip with an IF statement in the second script. This script is named BoolKeeper and is attached to an object named BoolKeeper. Maybe it’s not great that I named the game object and script the same name? Anyway, have a look.

using UnityEngine;
using System.Collections;

public class BoolKeeper : MonoBehaviour
{
    public bool rightRacketCollision;

     void Update ()
    {
        Debug.Log ("rightRacketCollision is " + rightRacketCollision);
    }
}

This is the second script that is a component of a different game object. In this script I try and use an IF statement to flip “rightRacketCollision” in the first script (to no avail).

using UnityEngine;
using System.Collections;

public class SetRightRacketBool : MonoBehaviour
{
    public bool test;
    bool rRacketCollision = GameObject.Find(BoolKeeper).GetComponent<BoolKeeper>().rightRacketCollision;
    
void Update ()
    {
        if (test = true)
        {
            rRacketCollision = true;
        }
        else
        {
            rRacketCollision = false;
        }
    }
}

I’m hoping that when I toggle the public “test” bool in the inspector (on script two), I should see my log (“rightRacketCollision is true/false”) from the first script. What am I doing wrong? Many thanks.

  1. In your second script, you should do your GameObject.Find in the start method. After you find the object store it in a variable and make sure what you got was not null, if it was then you will not be able to get any components and it will throw and error.
  2. BoolKeeper in the Find parameters needs to be in quotes because you are looking for a GameObject not a Type.
  3. Your in your If statement boolean expression you are using the wrong operator.

“test = true” will always just set test to true. then evaluate the new value of test which will always be true. Instead you will want to use “test == true” to check if test is the value true.

1 Like

Thanks so much for the quick response! I’m a bit confused though…

Here is the revised script.

using UnityEngine;
using System.Collections;

public class SetRightRacketBool : MonoBehaviour
{
    public bool test;

    void Start()
    {
        bool rRacketCollision = GameObject.Find("BoolKeeper").GetComponent<BoolKeeper>().rightRacketCollision;
    }

    void Update ()
    {
        if (test == true)
        {
            rRacketCollision = true;
        }
        else
        {
            rRacketCollision = false;
        }
    }
}

This is still not working. Isn’t it true that if I define the bool rRacketCollision with GameObject.Find in the Start method, it’s no longer a member variable and cannot be accessed by Update? Thank you so much for your help!

Your confuessed…
bool rRacketCollision = GameObject.Find("BoolKeeper").GetComponent<BoolKeeper>().rightRacketCollision; these line will return you the value of rightRacketCollision.
you don’t want that. What you want is reference of that script and then change bool value using that reference.
Other thing isbool rRacketCollision is Local variable. you won’t be able to use it Inside Update

Do it like these

using UnityEngine;
using System.Collections;
public class SetRightRacketBool : MonoBehaviour
{
    public bool test;
    private BoolKeeper _refBoolKeeper;
    void Start()
    {
       //first take the reference of that scipt
       _refBoolKeeper= GameObject.Find("BoolKeeper").GetComponent<BoolKeeper>();
    }
    void Update ()
    {
        if (test.== true)
        {
            _refBoolKeeper.rRacketCollision = true;
        }
        else
        {
            _refBoolKeeper.rRacketCollision = false;
        }
    }
}
2 Likes

@nelson218 : Perfect! This makes complete sense and it worked like a charm. Many thanks! I have a quick question though. When you declare the variable for the script reference (private BoolKeeper _refBoolKeeper) I’m confused why you have to use the name of the script (BoolKeeper) to indicate the the variable type is a class. I’m new to scripting obviously but is there not a generic variable type for class like there is for int, float, string, etc? In any case thank you so much.

1 Like

@JD_Designer see it’s just a name. You can call it anything from xyz or JD_Designer also. But its good practice that the variable name should be meaningful. And I use ref in front of any variable if its a reference of any custom class eg class BoolKeeper.
I didn’t understand what your trying say with int, float

Thanks. Yes, I understand that _refBoolKeeper is the name we are assigning to a variable that holds my script. My confusion is about the use of BoolKeeper when we declare that variable.

From what I understand so far, when declaring a variable you must first define the type/kind of variable it is.
Eg: int myInt = 5;
By inserting “int” we are declaring a variable that will be an integer. So what is happening when we insert BoolKeeper?

//we declare a bool and name it test
public bool test;

/*we declare a variable that will refference the script named BoolKeeper
and we give that variable the name _refBoolKeeper.  But why are we using the
name of my script (BoolKeeper)as the type of variable to be declared?  Especially
since we assign BoolKeeper (the name of my script) to _refBoolKeeper in Start
with GameObject.Find and GetComponent?*/
private BoolKeeper _refBoolKeeper;

BoolKeeper _refBoolKeeper just means its an instance of the BoolKeeper class.

So just like you could have
int x = 1;
int y = 2;

You could have
BoolKeeper bk1;
BoolKeeper bk2;

That way if you change something in bk1, it doesn’t change values in bk2.

1 Like

Thanks for the response! So sorry. I’m trying to wrap my head around this. So when we insert BoolKeeper as part of our variable declaration, we are creating a variation of my script BoolKeeper and naming it _refBoolKeeper. But if this statement means _refBoolKeeper = an instance of BoolKeeper, then why do I have to GetComponenet(); in Start?

You have to assign a value to your BoolKeeper variable, or in this case, your instance, which is on your object.
BoolKeeper bk1 doesn’t reference anything, it’s just a variable, so it’s not usable as it is. You need it to point towards something. You use getcomponent to tell it what it references.

If you have two objects with BoolKeeper on them

BoolKeeper bk1 = target1.getComponent();
BoolKeeper bk2 = target2.getComponent();

Would be an example of how you might use it. Now, you can do a singleton which would allow you to not use getComponent if you only have one copy of that script in your scene, but you’ll have to look that up if you’re interested.

The use of variables, classes, and instances are probably good things to learn since they are apart of many programming languages.

Thanks Brathnann! I’m going to have to let this percolate a bit, but I think it’s starting to click.