0

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;
        }
    }
}
Ruzihm
  • 19,749
  • 5
  • 36
  • 48
Botros
  • 3
  • 2
  • 1
    Try changing `public bool selected` to `public static bool selected`. – Guilherme May 03 '19 at 04:34
  • the simplest solution is to use static variable. And you can actually simplify your ShowRange() method: `public void showRange(){ foreach(var tile in tileRange) { tile.GetComponent ().enabled = selected; } }` – kolodi May 03 '19 at 08:54
  • This is about [tag:c#], not [tag:unityscript]. – Ruzihm Feb 26 '20 at 19:21

3 Answers3

1

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;

Josep
  • 676
  • 2
  • 8
  • 14
0

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.

  • public static gamemanager Instance; Do you mean (public static GameObject Instance;), because it gives me an error(gamemanager is not defined in unity monobehaviour) – Botros May 03 '19 at 11:26
  • your gamemanager should be a separate script. create a new script, name it gamemanager and then try the answer – Muhammad Farhan Aqeel May 03 '19 at 11:53
  • 1
    Someone who is asking a simple question should get a simple answer, telling them to copy-paste a Singleton pattern and use of multiple-scripts using a manager seems completly overkill to me. – Josep May 05 '19 at 10:12
0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Botros
  • 3
  • 2