Botros
Botros

Reputation: 3

Using same Boolean with multiple objects in Unity 2D

I have four game objects with the same script and the same bool, when one of them get hit by ray the bool state is true, but the function will not start because the other three game objects is set to false.

What I tried:

  1. the code works fine with the last object being instantiated
  2. if I disabled the script on the first object and re-enabled it again the function works fine on this object only
public bool selected;

void Start(){
    selected = false;
}

void Update(){
    showRange ();
}



public void showRange(){

    if (selected == true) {
        for (int i = 0; i < tileRange.Count; i++) {
            tileRange [i].GetComponent<SpriteRenderer> ().enabled = true;
        } 
    } else {
            for (int i = 0; i < tileRange.Count; i++) {
                tileRange [i].GetComponent<SpriteRenderer> ().enabled = false;
        }
    }
}

Upvotes: 0

Views: 1987

Answers (3)

Botros
Botros

Reputation: 3

The problem isn't in the selected bool, but it's in showRange() method. If the ray hit one object it will be selected and the other three will remain unselected ((that's because I use a list that stores the last hitted object, then the code works only for the object inside this list))

showRange() will not work with the selected object, because the method want all the four object to be selected, then it works (stupid method, I was unable to sleep because of it).

I managed to fix showRange() problem, using a new script that turns off all game objects script component and turns on the selected one script, this will make showRange() method unable to check the bool state of the other three objects.

For (Guilherme, misher and Josep) thank you for your help, I really appreciate it, but as it is shown above the problem wasn't in the Boolean.

For (Muhammad Farhan Aqeel) I believe your code should work, but I didn't manage to get it work maybe because I'm still new to programming.

Upvotes: 0

Josep
Josep

Reputation: 734

Simply use a static variable: What is the use of static variable in C#? When to use it? Why can't I declare the static variable inside method?

You would have:

public static bool selected;

Upvotes: 1

Muhammad Farhan Aqeel
Muhammad Farhan Aqeel

Reputation: 717

Try using gamemanager.

public static gamemanager Instance;
public bool selected;
private void Awake()
{
    Instance = this;
}

Your gamemanager can have a public bool variable "selected".

New start method:

void Start() {
gamemanager.Instance.selected = false;
}

New showRange function

public void showRange(){

if (gamemanager.Instance.selected) {
    for (int i = 0; i < tileRange.Count; i++) {
        tileRange [i].GetComponent<SpriteRenderer> ().enabled = true;
    } 
} else {
        for (int i = 0; i < tileRange.Count; i++) {
            tileRange [i].GetComponent<SpriteRenderer> ().enabled = false;
    }
}
}

I am sure this code can be improved. Let me know if it helps.

Upvotes: 0

Related Questions