Hi,
In Unityscript we can refer to a static variable in another script by adding the script name in front.
ex. otherscript.myvar
Is this also possible in C# and how is it done ?
thanks.
Hi,
In Unityscript we can refer to a static variable in another script by adding the script name in front.
ex. otherscript.myvar
Is this also possible in C# and how is it done ?
thanks.
Add a static variable to your class. You can only access it from script. Their values cannot be set at design time.
public class MyClass : MonoBehaviour
{
public static Transform SomeOtherObject;
public static float GlobalDelay;
private void Update()
{
// Some code
}
}
Regards
/Klum
Should be identical in C#.
Rather than script, think of it as the class.
myclass.myvar
Every instance of myclass or a reference of myclass should return the same myvar if its a static variable.
You can’t call static properties from an instance of the class.
Correct:
MyClass.MyStatic;
Incorrect:
MyClass myClass = new MyClass();
myClass.MyStatic; // Won't work.
Regards
/Klum
Thanks, works fine ![]()