0

I'm looking to trigger a public bool setup in a LevelManager Script from another script.

so when something happens on script B, the bool in script A switches to true.

Below, are 2 scripts where levelRestart.cs is a trigger, when a player hits, it needs to access the public bool in the LevelManager script and say startGame is true.

Can someone help?

LevelManager.cs

public bool startGame = false;

void Update ()
    {

        if (startGame == true)
        {
            SpawnKonyaku();
            startGame = false;
        }
    }

LevelRestart.cs

void OnTriggerEnter2D(Collider2D col)
    {
        if (col.gameObject.tag == "Player")
        {            
            levelManager.startGame = false; //<- This is where, i need help    
        }
    }

RestartLevel Script to trigger public bool startGame to be true

Roman Pokrovskij
  • 9,449
  • 21
  • 87
  • 142
ludo japn
  • 59
  • 1
  • 2
  • 7
  • 4
    Possible duplicate of [How to access a variable from another script in another gameobject through GetComponent?](https://stackoverflow.com/questions/26551575/how-to-access-a-variable-from-another-script-in-another-gameobject-through-getco) – Draco18s no longer trusts SE Jan 28 '19 at 21:45

2 Answers2

0

Ok, i figured this out myself, im gonna write the answer here, to add to the many other script questions out there.

I added these variables to pull from script A

public GameObject _LevelManager;
private LevelManager script;`

than on Void Start i added

void Start ()
{
    script = _LevelManager.GetComponent<LevelManager>();
}

and it acctually called the bool when i ran the triggerenter method

public void OnTriggerEnter2D(Collider2D col)
{
    if (col.gameObject.tag == "Player")
    {            
        script.startGame = true;         
    }
}
ludo japn
  • 59
  • 1
  • 2
  • 7
  • It's not good to directly modify variables from script to script. You should use a method instead. Something like: `public void StartGame() { startGame = true }`. BUT from what it looks like for me you don't really need this startGame variable at all. Maybe just call the `SpawnKonyaku()` method directly and let it set your boolean if you really need it for something else? – Nicolas Jan 29 '19 at 09:07
0

You can trigger that bool startGame by keeping it as static i;e public static bool startGame. So that you can trigger with LevelManager directly.