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 CHRISYS · Nov 21, 2013 at 02:05 AM · networkinginstantiatenetworknullreferenceexception

Networking-multplayer issue

hi ther guys first thanks for those who read and answer my quesions ..!! :) now the isue i have is this:

so i network.instantiate my player o the level like this in the OnNetworkLoadedLevel function that comes with unity:

 void OnNetworkLoadedLevel ()
 {
         int SpawnIndex = Random.Range(0, respawns.Length);
         
         GameObject this_player = Network.Instantiate(Player, 
                                                      respawns[SpawnIndex].transform.position,
                                                      Quaternion.identity, 0) as  GameObject;
         
         this_player.GetComponent<IsRemotePlayer>().UserName = this_player_nametag;
 }


UserName and this_player_nametag are strings but as you can se UserName is on a script attached to the player prefab i instantiate , the isue is here on this line:

this_player.GetComponent().UserName = this_player_nametag;

it gives me this error on console:

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

  • so the question is why s this happening? i call properly the OnNetworkLoadedLevel() and even the player prefab spawns and sete everything properly, but the error that display the console is like there was no player gameobject at all right??

plz help me with this..!!

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 supernat · Nov 21, 2013 at 04:59 AM 0
Share

Not necessarily. this_player could well be valid! but it may not have an IsRemotePlayer script assigned to it. Is this script showing in the editor when you click on the player game object? I would recommend checking both the value of this_player for null and the value of GetComponent return for null anyway.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by KiraSensei · Nov 21, 2013 at 02:34 PM

Several possibilities : this_player is null, this_player does not contain a script IsRemotePlayer, or the script IsRemotePlayer has no public declared variable named UserName

You need to check if everything exists :

 void OnNetworkLoadedLevel ()
 {
     int SpawnIndex = Random.Range(0, respawns.Length);
  
     GameObject this_player = Network.Instantiate(Player, 
                                    respawns[SpawnIndex].transform.position,
                                          Quaternion.identity, 0) as  GameObject;
  
     if (this_player)
     {
         Debug.Log("I have my player");
         IsRemotePlayer myScript = this_player.GetComponent<IsRemotePlayer>() as IsRemotePlayer;
         if (myScript)
         {
             Debug.Log("I have my script");
             string myCurrentName = this_player.GetComponent<IsRemotePlayer>().UserName;
              Debug.Log("I have my user name");
              this_player.GetComponent<IsRemotePlayer>().UserName = this_player_nametag;
         }
     }
 }
Comment
Add comment · Show 1 · 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 KiraSensei · Nov 25, 2013 at 07:42 PM 0
Share

Could you just tell what was the problem ?

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

19 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 avatar image avatar image

Related Questions

How to create a object that can only be seen by one player? 0 Answers

Instantiate prefab and passing parameter 1 Answer

Unet NetworkServer.Spawn() not working 5 Answers

ChannelBuffer buffer limit of 16 packets reached on sending images 1 Answer

How can I spawn a GameObject on all players in my multiplayer game? 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