Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by QuestionAsker · Aug 05, 2013 at 04:27 PM · objectnullreferenceexceptiongetcomponentinstancecalling

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image QuestionAsker · Aug 05, 2013 at 04:48 PM 0
Share

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?

avatar image pako · Aug 05, 2013 at 05:23 PM 0
Share

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();

avatar image pako · Aug 05, 2013 at 05:31 PM 0
Share

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.

avatar image Joyrider · Aug 05, 2013 at 06:05 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges