How to reference A variable in a Namespace / partial class.

Hi, So I purchased an asset, and I would like to reference a variable in this script. But I cant seem to be able to do that. I think its because this script has a Namspace / or the variable is in a partial class. Both of which are new to me.

This is a snip of the code; I would provide more of the code but I am not sure if thats against the rules considering its from the asset store;

namespace VolumetricFogAndMist {


    public enum MASK_TEXTURE_BRUSH_MODE {
        AddFog = 0,
        RemoveFog = 1
    }


    public partial class VolumetricFog : MonoBehaviour {

            //Variable that i want to reference is here (I have made it public).
} 
}

Accessing a public instance variable on VolumetricFog should be very simple:

var volumetricFog = FindObjectOfType<VolumetricFogAndMist.VolumetricFog>();
Debug.Log("Variable value: "+volumetricFog.variable);

If that doesn’t work for you, it could be that the asset is using assembly definition files. If that is the case, you need to create an assembly definition file in the folder where your script is (or any parent folder), and add a reference to the assembly definition file of the asset.

2 Likes

Worked Like a Charm bro, thanks so much

1 Like

What

How about in C#?

When working with non-Component classes you don’t add the class to a GameObject and can’t use FindObjectOfType to find find an existing instance. Instead you simply use the new operator to create an instance. Then you can access the variables within that instance just like you would with a component.

var createdInstance = new ExampleClass();

createdInstance.exampleVariable = true;

Debug.Log("Variable value: " + createdInstance.exampleVariable);

Here’s some more information: