0

I'm having trouble finding out how to share variables between different c# files. I'm relatively new to coding in this language and i don't know how to access a Boolean from one file in another.

I can provide code if needed, i'm using unity if that matters. I'm being told that the variable doesn't exist in current context. ask if more info is needed i don't really know what you nee to answer this.

this is the first file

using System.Collections;
using System.Collections.Generic;
using UnityEngine;



public class floorAndWallCheck : MonoBehaviour {
    public bool isGrounded = false;

    public void OnTriggerEnter2D(Collider2D floor){
        if (floor.tag == "floor") {
            isGrounded = true;
        }else {
            isGrounded = false;
        }
    }
}

this is some lines from second file

void Update () {
    //left
    if (Input.GetKey("a")) {
        player1.transform.Translate(Vector3.left * Time.deltaTime * speed);
    }
    //right
    if (Input.GetKey("d")) {
        player1.transform.Translate(Vector3.right * Time.deltaTime * speed);
    }
    //up
    if (Input.GetKey("w") && isGrounded == true) {
        player1.transform.Translate(Vector3.up * Time.deltaTime * jumpForce);                       
    }
  • 2
    You need to learn a lot more about C# and programming. [Here](https://learn.microsoft.com/en-us/dotnet/csharp/) is a good place to start. There is no such thing as "C# file" - all this "files" are compiled into an assembly (usually one) and after that they exist in a .net CLR (Common Language Runtime). – vasily.sib Jan 28 '19 at 02:57
  • Thanks! i Will be sure to check it out. – Bailey Anderson Jan 28 '19 at 03:01
  • Possible duplicate of https://stackoverflow.com/a/25932315/7111561 .. this was answered so many times already – derHugo Jan 28 '19 at 05:21
  • Possible duplicate of [Accessing a variable from another script C#](https://stackoverflow.com/questions/25930919/accessing-a-variable-from-another-script-c-sharp) – Muhammad Faizan Khan Jan 28 '19 at 11:35

2 Answers2

2

Since it seems that you might want to check isGrounded for multiple players I would not use static as mentioned in the other answer since then it is static for every instance of floorAndWallCheck.


Instead you have to access the member of the according instance.

Assuming the floorAndWallCheck component is e.g. attached to the player1 object you can do

private floorAndWallCheck player1FloorCheck;

private void Awake ()
{
   player1FloorCheck = player1.GetComponent<floorAndWallCheck>(); 
}

private void Update()
{

    //...

    Input.GetKey("w") && player1FloorCheck.isGrounded)
    {
        // ...
    }
}

or instead of using GetComponent you can make it either public

public floorAndWallCheck player1FloorCheck;

or use [SerializeField]

[SerializeField] private floorAndWallCheck player1FloorCheck;

and reference the according component in the Inspector (via drag & drop)


Find more ways for accessing other classe's members here

derHugo
  • 83,094
  • 9
  • 75
  • 115
1

Here it looks like you want to use static

public class floorAndWallCheck : MonoBehaviour {
    static public bool isGrounded = false;

    public void OnTriggerEnter2D(Collider2D floor){
/// etc

Then in the other file

if (Input.GetKey("w") && floorAndWallCheck.isGrounded == true) {

https://unity3d.com/learn/tutorials/topics/scripting/statics

The problem is that this will make the variable global to all instances of a floorAndWallCheck

I don't think you want that even if you are asking for it in your question. Probably what you really want to do is not "share the variable" but instead have a variable of that type be a parameter to whatever function you were using in the 2nd file. In this way you can "pass" the data from one place to another.

Hogan
  • 69,564
  • 10
  • 76
  • 117
  • Thank you for the help. I will test it tommorrow after work. – Bailey Anderson Jan 28 '19 at 03:09
  • It doesn't have to be static necessarily .. I would rather recommend to access the correct instance of `floorAndWallCheck` – derHugo Jan 28 '19 at 07:04
  • @BaileyAnderson - Hogan's answer is more about what to avoid. You shouldn't use `static` is the bottom-line - it will cause you problems. You should find out how to reference the specific instance. A full [mcve] would be useful in your question. – Enigmativity Jan 28 '19 at 07:19
  • Static is usually not the best solution, particularly with beginners. But in this case, it is fine since there would be only one floorAndWallCheck component over the whole application. Now if that component is to be used on multiple objects then static is no longer valid. – Everts Jan 28 '19 at 12:34