简体   繁体   中英

Trouble Sharing Variables between files in c#

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);                       
    }

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 eg 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM