New to unity, need help! *Assigning and using Boolean variable*

Hey there im new to unity so please forgive anything i say thats wrong.
Right now im trying to set up a Key script,
with a cube mesh, and clicked istrigger
I have made a new C# script, I would like to make a public boolean variable,
so if the player collides with the key trigger, the boolean variable changes and a door object opens up somewhere else in the level

does this all seem fine so far?

using UnityEngine;
using System.Collections;

public class Key : MonoBehaviour {
public boolean Key1Trigger = false;

// Use this for initialization
void Start () {
gameObject.renderer.material.color = Color.red;
}

// Update is called once per frame
void Update () {

}

code should compile. as far as if it’s right or wrong, depends on what you’re trying to do.

im trying to set up a public boolean that i can call and use in a different script

Sure. That will work. You might have to do a little more work to access that script from another script but you’re on the right track.

public bool myBoolean

It’s bool, not boolean.
you might want to do take a look at the C# syntaxes.

Thanks!
How would i go about accessing that bool in another script?
script 1:
bool = bool
bool = false

if player collides then bool = true

script 2:
if bool = true then
{}

Well, you have to get a reference to that script through Unity.

So, in Script2 you could do:

Public Script1 otherScript;

Then in the editor, drag the GameObject that has a Script1 component from the scene hierarchy onto the little rectangle in the inspector and that will build the reference for you.

There’s other ways to do it, as well. Then inside of Script2, to check Script1’s bool:

if (otherScript.bool == true) // yay!

this, but don’t call ur boolean ‘bool’.
Since that’s already the type that you’re declaring it.
to avoid confusion, call it myBoolean. Or a more descriptive name that indicates what the on-off state means.

was just calling it as an example, i would never call a bool boolean, or an integer int
far too confusing in the long run!

Sounds like you need to put all this out of your head and visit this page a while first.

Hope that didn’t sound rude that is not what I intended.

2 Likes

Its alright i completely agree, im so lost at the moment i need to go back to the basics