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;
        }
    }
}
3
  • 1
    Try changing public bool selected to public static bool selected. Commented May 3, 2019 at 4: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<SpriteRenderer> ().enabled = selected; } } Commented May 3, 2019 at 8:54
  • This is about c#, not unityscript. Commented Feb 26, 2020 at 19:21

3 Answers 3

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;

Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

public static gamemanager Instance; Do you mean (public static GameObject Instance;), because it gives me an error(gamemanager is not defined in unity monobehaviour)
your gamemanager should be a separate script. create a new script, name it gamemanager and then try the answer
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.