Hello all. I need to access a global variable from a script via a Raycast Hit, but I can’t get it done due to a knob in my head: let’s say “Object1” Raycasts and hits “Object2”. Object2 has a script “Somescript” attached with a static global variable. I tried for example:
if (Physics.Raycast(transform.position,direction,hit,range))
{
var getname = hit.collider.getComponent(Somescript).GlobalVariableName;
print(getname);
}
and also some other variations with “GetComponent” e.g. var getname = hit.collider.transform.parent.getComponent(Somescript).GlobalVariableName - but still only getting error messages. I’m obviously stuck simply trying to get the “GameObject” anyways… Please help me 
If it’s static it doesn’t work with an instance.
No GetComponent.
Just
if(Physics.Raycast(…))
{
var getname = Somescript.GlobalVariableName;
pring(getname);
}
Static variables are not owned by any particular instance and is called by the Class name as a whole.
Oh my god, thaaaankssss a lot - That was it! I was already getting a hot head trying to figure out what is wrong.
Hmmm, these lines are completely independent from the hitObject, which is a problem…
Let’s say I have different factions in a game - and whenever units of factions come close to each other they determine via Raycasts to one another if the other unit belongs to the same faction or not. I initially thought I could do this via storing a global variable (a different number for each faction) in a script used by a Prefab-Object. But “var getname = Somescript.GlobalVariableName;” will not work this way, because its independent from the hitObject. How can I check this "property"now?
Don’t use the static keyword and set the variable when instantiating the Prefab.
Static is for variables that only have 1 set across all instances. This is good for storing a list of all instances of the class, or of getting a global amount.
regular variables are the ones acessed by GetComponent and can be set on an instance per instance basis.
And if it’s not static it means your first example would be accurate to getting the variable