- Home /
get component null reference
private int mapSelected; private mainmenu mainMenu;
void Awake() { mainMenu = GetComponent(); } > > void Start() { mapSelected = > mainMenu.champSelected; }
Object reference not set to an instance of an object introbeforegame.Start ()
what's wrong?
I don't understand your question. Please remake it with more details and with your script.
Answer by hamstar · Sep 30, 2013 at 08:00 PM
You need to supply a type to GetComponent. See the documentation. If the component you want is a script you have created, use that as the type parameter.
e.g. If you named a script "MenuScript":
mainMenu = GetComponent(MenuScript); // JS
mainMenu = GetComponent<MenuScript>(); // C#
Answer by Weither · Sep 30, 2013 at 09:10 PM
Script 1:
if (GUI.Button (new Rect (10,175, 220,40), "option1", GUI.skin.GetStyle("button")))
{
champSelected = 4;
Application.LoadLevel ("Harborintro");
}
if (GUI.Button (new Rect (10,175, 220,40), "option2", GUI.skin.GetStyle("button")))
{
champSelected = 4;
Application.LoadLevel ("Harborintro");
}
Script nr2
Load champSelected from script 1 and save it in "mapSelected"
What is the actual name of Script1? Is it added to the same Game Object as the script you posted in your question? Is champSelected a public variable? Please edit your question rather than putting more info in an answer.