Hi, I have this in script#1:
public static int Usetype = 0;
and this line I have in script #2:
public int type = 0;
Namebuild.GetComponent<UseMainBuildType>().Usetype = type;
The last on gives an error: Static member `UseMainBuildType.Usetype’ cannot be accessed with an instance reference, qualify it with a type name instead
I saw similar questions and solutions but they are some different and I can’t use them
Static members belong to the class itself and not to an instance of that class. GetComponent retrieves a reference to an instance. To access a static member you only need to use the class name:
UseMainBuildType.Usetype = type;
Keep in mind that since static members don’t belong to a specific instance, the variable only exists once and is shared between all instances of that class.
I am using same but its working…
See Class First
public class test1 : MonoBehaviour {
public static int a = 1;
}
and Class Second
public class test2 : MonoBehaviour {
void Start () {
if(test1.a == 1)
Debug.Log("Working...");
}
}