Search Unity

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

Discussion in 'Scripting' started by LukeDodds, Oct 24, 2014.

  1. LukeDodds

    LukeDodds

    Joined:
    Oct 24, 2014
    Posts:
    46
    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 () {

    }
     
  2. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
    code should compile. as far as if it's right or wrong, depends on what you're trying to do.
     
  3. LukeDodds

    LukeDodds

    Joined:
    Oct 24, 2014
    Posts:
    46
    im trying to set up a public boolean that i can call and use in a different script
     
  4. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
    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.
     
  5. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Code (CSharp):
    1. public bool myBoolean
    It's bool, not boolean.
    you might want to do take a look at the C# syntaxes.
     
  6. LukeDodds

    LukeDodds

    Joined:
    Oct 24, 2014
    Posts:
    46
    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
    {}
     
  7. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
    Well, you have to get a reference to that script through Unity.

    So, in Script2 you could do:
    Code (CSharp):
    1. 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:

    Code (CSharp):
    1. if (otherScript.bool == true) // yay!
     
  8. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    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.
     
  9. LukeDodds

    LukeDodds

    Joined:
    Oct 24, 2014
    Posts:
    46
    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!
     
  10. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    LukeDodds and Magiichan like this.
  11. LukeDodds

    LukeDodds

    Joined:
    Oct 24, 2014
    Posts:
    46
    Its alright i completely agree, im so lost at the moment i need to go back to the basics