- Home /
Static member cannot be accessed
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
So what is Use$$anonymous$$ainBuildType? Is this the class or an instance?
Answer by Bunny83 · May 10, 2014 at 09:16 AM
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.
Answer by VIPINSIRWANI · May 10, 2014 at 08:40 AM
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...");
}
}
yes, that works, but Oh man. I'm sorry, I wrote not those lines with which I have a problem! (those were from another script). In Script #2 the line is:
public int type = 0;
Namebuild.GetComponent<Use$$anonymous$$ainBuildType>().Usetype = type;
and here I have this error: Static member `Use$$anonymous$$ainBuildType.Usetype' cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d I'm sorry but I still haven't solved it :)