Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Accessing public bool from other script

Discussion in 'Scripting' started by Tyraxx, Aug 2, 2014.

  1. Tyraxx

    Tyraxx

    Joined:
    Jul 30, 2014
    Posts:
    13
    Hi everybody,

    I know many ppl already asked this, but these threads are usually specified on their problem. Anyways,...

    I have a publi bool in one script, and I only want to ask an if-statement with this bool in another script,
    how do I do this?
     
  2. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    To access any public information from another script, you must use class inheritance in unity using GetComponent. Check out my tutorial link in my signature and watch video 8 - GetComponent.

     
  3. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    There are two other ways to do this.

    1. Use the singleton pattern or public static variable.
    2. Setup a public link in the testing script and link them together in the editor. Best when the elements are already in the scene.
     
  4. Tyraxx

    Tyraxx

    Joined:
    Jul 30, 2014
    Posts:
    13
    Can someone give me an example code please?

    @SubZeroGaming
    No offense, but the audio quality of the video is kinda fuzzy

    EDIT: It's C#, BTW
     
    Last edited: Aug 2, 2014
  5. gk104

    gk104

    Joined:
    Apr 25, 2014
    Posts:
    57
    Just write the following line at the begining of the second script:

    public YourScript script;

    YourScript = instead of writing it, write the name of the script that you want to access in other script, then, if you want to use the variables, write:

    script.variableName

    if-statement:

    if(script.variableName==true/false)
    {

    }
     
    VarunLanjhara likes this.
  6. gk104

    gk104

    Joined:
    Apr 25, 2014
    Posts:
    57
    forgot to mention, after you wrote the first line, go to the inspector of the new script and attach the script there.
     
  7. Tyraxx

    Tyraxx

    Joined:
    Jul 30, 2014
    Posts:
    13
    Hmm, so like this?

    Code (csharp):
    1. public class Script2 : MonoBehaviour {
    2.  
    3. Script2 = Script1;
    4.  
    5. if (Script1.var < 0) {
    6.  
    I don't quite get it, probably the "Script2 = Script1;" part is wrong.
    Help me out please.

    Thanks ia
     
  8. Philip-Rowlands

    Philip-Rowlands

    Joined:
    May 13, 2013
    Posts:
    353
    Try this instead:
    Code (csharp):
    1.  
    2. public class Script2 : MonoBehaviour
    3. {
    4.      public Script1 myScript;          // then assign this on the Inspector
    5.      
    6.      if (myScript.myBoolean == true)    // myBoolean is the bool you wish to compare
    7.     {
    8.          // your code here
    9.     }
    10. }
    11.  
     
  9. Tyraxx

    Tyraxx

    Joined:
    Jul 30, 2014
    Posts:
    13
    OK, I'm very close to it.

    Now MonoDevelop at least doesn't mark anything red anymore, and I have a slot in the inspector where I can drag in the script. But for some reason I cannot do it, neither drag or drop nor via "Select Control".

    public Control MyScript;
    ("Control" is the name of my other script)

    PS: Oh, and BTW, you posted your last post twice
     
  10. Philip-Rowlands

    Philip-Rowlands

    Joined:
    May 13, 2013
    Posts:
    353
    Thanks for the reminder, I hadn't noticed it. :oops:

    When you try assigning your script in the Inspector, does it show under the Scene tab? If not, you'd need to add it to a GameObject in the scene. If it is, then could you post the script you're working on?
     
  11. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    If you watched the video, and it doesn't matter if it's javascript or C# that you're working... Stop spoon feeding him, people...

    Create a private handle...

    private CLASSNAME _referenceName;

    Now you need to assign that handle to the component you want to access.....First find the gameObject, then assign the component. Unity sees everything as a component.

    void Start(){
    _referenceName = GameObject.Find("name of object").GetComponent<CLASSNAME>();
    }

    now you can type:

    _referenceName.variable.
     
  12. gk104

    gk104

    Joined:
    Apr 25, 2014
    Posts:
    57
    no, it's not correct.. it's should be like that:

    (write it in the second script)

    public XXX script;

    XXX = name of the script that you want to access

    then go to the inspector of the new script, and attach and original script in there.
     
  13. Tyraxx

    Tyraxx

    Joined:
    Jul 30, 2014
    Posts:
    13
    void Start () {
    SpriteAnimation = GameObject.Find("Player").GetComponent<Control>();
    }

    Both scripts are on the same gameObject.
    "SpriteAnimation" = name of the current script
    "Control" = name of the script I want to access.

    The Console gives me: "The left-handed side of an assignment must be a variable, a property or an indexer"

    Also, my "inspector" doesn't seem to refresh anymore, since I deleted the old variable but it's still there.
    I tested by just setting up a quick public bool, but it is not shown in the inspector.
     
  14. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Watch my youtube video...

    And actually watch it. It tells you that if you're on the same gameObject as the script, you can just do GetComponent.

    If you're on the Control script... and you need access to the sprite animation script....


    public class Control : MonoBehavior
    {
    private SpriteAnimation _anim;

    void Start()
    {
    _anim = GetComponent<SpriteAnimation>();
    }
    }

    void Update()
    {
    if (_anim.somebool == true){

    }
    }