• 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
-2
Question by RainKiller27 · Jul 08, 2015 at 11:15 AM · nullreferenceexception

NullRefenceException: Object reference not set to an instance of an object

MenuNetwork.SpawnMyPlayer() (at Assests\scripts\MenuNetwork.cs:34

 using UnityEngine;
 using System.Collections;
 
 public class MenuNetwork : Photon.MonoBehaviour {
 
     public GameObject spawnPos;
     public GameObject _StandbyCamera;
 
     void Start () {
         PhotonNetwork.ConnectUsingSettings("0.1");
     }
 
     void OnGUI () {
         GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
     }
 
     void OnJoinedLobby() {
         PhotonNetwork.JoinRandomRoom();
     }
 
     void OnPhotonRandomJoinFailed() {
         PhotonNetwork.CreateRoom(null);
     }
 
     void OnJoinedRoom() {
         Debug.Log ("Connected to Room");
         SpawnMyPlayer();
     }
 
     void SpawnMyPlayer() {
         GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate("Player", spawnPos.transform.position, spawnPos.transform.rotation, 0);
         _StandbyCamera.SetActive(false);
 
         ((MonoBehaviour)myPlayerGO.GetComponent("MouseLook")).enabled = true;
         ((MonoBehaviour)myPlayerGO.GetComponent("CharacterMotor")).enabled = true;
         ((MonoBehaviour)myPlayerGO.GetComponent("PlayerGUI")).enabled = true;
 
         myPlayerGO.transform.FindChild("Main Camera").gameObject.SetActive(true);
     }
 }
Comment
Add comment · Show 1
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 Hellium · Jul 08, 2015 at 01:46 PM 0
Share

Tell, me what is complicated in making research and reading the FAQ ? SERIOUSLY ?

http://answers.unity3d.com/questions/47830/what-is-a-null-reference-exception-in-unity.html

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by komodor · Jul 08, 2015 at 11:17 AM

so it's this line:

 ((MonoBehaviour)myPlayerGO.GetComponent("MouseLook")).enabled = true;

i'd say it's the MonoBehaviour casting, just remove (MonoBehaviour)

if the problem persists then you miss MouseLook component on myPlayerGO

Comment
Add comment · Show 3 · 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 Thomas-Mountainborn · Jul 08, 2015 at 11:21 AM 0
Share

If the casting would be the issue, there would be a cast exception. Only is he used the "as" keyword and it returned null (object as $$anonymous$$onoBehaviour), the casting could be the cause in this case.

http://stackoverflow.com/questions/132445/direct-casting-vs-as-operator

avatar image RainKiller27 · Jul 08, 2015 at 05:00 PM 0
Share

Yeah it doesn't seem to work, no matter what I do I tired everything you said but still getting the error when I load the game first player works fine but when the second joins they are both the same player and just glitches out?

avatar image RainKiller27 · Jul 08, 2015 at 05:05 PM 0
Share

yeah must of been the monobehaviour i'll have to rewrite them lines and find a different way of doing the same thing thanks for the help.

avatar image
1

Answer by Thomas-Mountainborn · Jul 08, 2015 at 11:20 AM

If GetComponent returns null, as it does in your case, it means the desired component isn't attached to your game object.

Also, just as a general tip, you really really really shouldn't use strings to do GetComponent. Instead, use the generic syntax:

 GetComponent<MouseLook>()

, or alternatively GetComponent(typeof(MouseLook)). That way, if you change the name of the class, these names will be updated automatically. They won't change in your string based approach, which will in turn lead to null reference exceptions because the desired type could not be found on the game object, because it does not exist.

Comment
Add comment · Show 2 · 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 RainKiller27 · Jul 08, 2015 at 05:00 PM 0
Share

Yeah it doesn't seem to work, no matter what I do I tired everything you said but still getting the error when I load the game first player works fine but when the second joins they are both the same player and just glitches out?

avatar image RainKiller27 · Jul 08, 2015 at 05:05 PM 0
Share

yeah must of been the monobehaviour i'll have to rewrite them lines and find a different way of doing the same thing thanks for the help.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException: Object reference not set to an instance of an object 0 Answers

How to setup code so it can recompile without null exception while running 1 Answer

Why am i getting a null reference exception? 1 Answer

NullReferenceException: Object reference not set to an instance of an object 1 Answer

animation[""] always null 0 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