- Home /
How to access public variable in namespace
Hello, I am working with Platformer Character 2D from the Standard Assets. In project pane I created a new script with public static variable bool. I want to access this public bool which is in a public class in the script of the 2D character which is in a namespace UnityStandardAssets._2D.
In the player script when I type the name of my public class it just do not show up... Any solutions?
Example: My public class:
public class Bumper : MonoBehaviour { public static bool bumper; }
Script of the 2D character from the standard assets where I would like to access a variable bumper:
namespace UnityStandardAssets._2D { public class PlatformerCharacter2D : MonoBehaviour { // here I want to acces my public static bool bumper. }
Example: $$anonymous$$y public class:
public class Bumper : $$anonymous$$onoBehaviour
{
public static bool bumper;
}
Script of the 2D character from the standard assets where I would like to access a variable bumper:
namespace UnityStandardAssets._2D
{
public class PlatformerCharacter2D : $$anonymous$$onoBehaviour
{
// here I want to acces my public static bool bumper.
}
Answer by Bunny83 · Aug 01, 2015 at 06:00 PM
I guess you have the PlatformerCharacter2D script either inside "Standard Assets" or "Plugins" but your "Bumper" script outside. Things in those folders belong to a different compilation group. They are compiled before your actual scripts. Things in those folders must not have any dependencies to classes outside those folders. If PlatformerCharacter2D class needs access to your Bumper script, either move your Bumper script into Standard Assets as well or move your PlatformerCharacter2D class out of the Standard Assets folder.
Yeah, it is as you say. I put my script into Standard Assets folder and now that script is visible. How come? There must be any other way. I want to have my staff nice and neat. Is it possible to access my scripts within Standard Assets scipts?
I may be missing something but I don't understand why you are editing Standard Assets at all. Try leaving that folder alone, and putting all your own code elsewhere.
Answer by Hexer · Aug 01, 2015 at 09:33 AM
method : Scriptname.variablename.
if the name of the script is X and the variable in that script that you want to access is y, you refer it in another script as X.y
NameOfScript.bumper
If you are still confused here's a link to Unity Learn tutorial about Statics : https://unity3d.com/learn/tutorials/modules/intermediate/scripting/statics
And a link to namespace https://unity3d.com/learn/tutorials/modules/intermediate/scripting/namespaces?playlist=17117
Yeah, I get it. But in the namespace the name of my script is not visible.
I still don't know how to reference my variable bumper. I don't know what namespace it is and which using to add. Can anybody tell me according to my example above how to access the variable bumper in namespace UnityStandardAssets._2D ??
bool bumper = Bumper.bumper;
No, the name of the script "Bumper" is not visible in the namespace. It is in a different folder of o project - does it matter?
Answer by tpainton · Dec 22, 2016 at 07:38 PM
You need to include the name space. So any script that accesses the target script needs
using UnityStandardAssets._2D;
Your answer
Follow this Question
Related Questions
error CS0246: The type or namespace name `rect' could not be found. 1 Answer
error CS0246: The type or namespace name `List' could not be found. 1 Answer
Namespace Functions not being recognized 0 Answers
Can't access another script, think it may be due to namespace/public class ? 1 Answer
Inspector references and scripts with the same name 0 Answers