- Home /
NullReferenceException: Object reference not set to an instance of an object .....
NullReferenceException: Object reference not set to an instance of an object MainMenu.OnGUI () (at Assets/GameAssets/Scripts/MainMenu.cs:17)
I have a method in Player.cs
public void spawnBlankPlayer ()
{
GameObject playerInstance = (GameObject)Instantiate(playerPrefab, spawn.position, spawn.rotation);
}
When i call it from MainMenu
using UnityEngine;
using System.Collections;
public class MainMenu : MonoBehaviour
{
private Player player;
void Awake ()
{
player = GetComponent<Player>();
}
void OnGUI () {
GUI.Box(new Rect(10,10,100,90), "Control Panel");
if(GUI.Button(new Rect(20,40,80,20), "Spawn me"))
{
player.spawnBlankPlayer();
}
if(GUI.Button(new Rect(20,70,80,20), "Spawn gun")) {
//Application.LoadLevel(2);//irrelevant line
}
}
}
And the error at the top is returned. I am very willing to try an alternative route to what I am doing, if you know a better one. Any help is greatly appreciated. Thank you.
EDIT:
Ok so I mixed things from all the very helpful comments, and want to post how i got it to work incase anyone else wants to know.
In PLAYER.CS
public Transform spawn;
public GameObject playerPrefab;
public void spawnBlankPlayer ()
{
GameObject playerInstance = (GameObject)Instantiate(playerPrefab, spawn.position, spawn.rotation);
}
I have written a line to spawn an instance of a player.
In MAIN MENU.CS public Player player; GameObject spawner;
void Awake ()
{
spawner = GameObject.Find("Player Spawn Point");
player = spawner.GetComponent<Player>();
}
void OnGUI () {
GUI.Box(new Rect(10,10,100,90), "Control Panel");
if(GUI.Button(new Rect(20,40,80,20), "Spawn me"))
{
player.spawnBlankPlayer();
}
}
And that works.
Answer by Joyrider · Aug 05, 2013 at 04:44 PM
I'm guessing your player stays null, so it throws you an error when you try to call a function on it. Are you sure you have a component on your object? And I'm wondering if you shouldn't use gameObject.GetComponent();
But anyway, check if(player != null) before calling your function at line 17.
That is the issue. However now I realize i can't spawn the player this way, because only the box(map) exists. I'm trying to spawn in the prefab when the button is clicked. I'm lost, do you have any idea how i would do this?
Do you mean that Player.cs is attached on the prefab you are trying to spawn? It sounds like it.
Try the following:
a. create an empty game object in your scene and rename it -say- "Spawner". Then attach the Player.cs script on it.
b. Then in $$anonymous$$ain$$anonymous$$enu get a reference to the "Spawner" GameObject. e.g. use GameObject spawner = GameObject.Find("Spawner");
c. And then use player = spawner.GetComponent();
Sorry, but I've been trying to input the GetComponent part of step 'c' similar to your line 10 of $$anonymous$$ain$$anonymous$$enu, but the system just deletes the 'Type' declaration inside the ... anyway, I think you get the general idea.
Euh, what pako said... But if you have nothing in your scene except your skybox... what is your $$anonymous$$ain$$anonymous$$enu script attached to? If it is attached to an object in your scene, you can just add the player component to that same object (and your script should work); it all depends what you want to do, and how complex your app is going to become.
If we didn't get what you mean.. I guess you should try to expand on what you are trying to do exactly, because we're guessing a little here.
Your answer
Follow this Question
Related Questions
NullReference Exception when adding a class 2 Answers
Pause menu error NullReferenceException:Object reference not set to an instance of an Object .js 1 Answer
NullReferenceException: Object reference not set to an instance of an object 0 Answers
The infamous: Object Reference not set to an instance of an object 1 Answer
" NullReferenceException: Object reference not set to an instance of an object" 3 Answers