Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Nakou · May 13, 2016 at 12:08 PM · unity 5networkingnullreferenceexceptionreference-other-objectnetwork instantiate

Reference lost in NetworkEntity

Hello everyone, I'm a newbie in the use of the UNET and I encounter some issue I quite not understand :

My player is a Tank which can shoot "Shells". So, my GameObject "Tank" has a component script named "Player" which contains Id and nickname of the player, stuff like that.

In the "Shell" GameObject, I have a script "Shell" where I've put a property name "Player".

When I shoot the shell, I inject, just after the Instantiate(), the reference of the object the Player into the property of the same name into the shell, so I can track who shot what on the map.

This is the code :

 [Command]
     void CmdShoot(){
         GameObject shell = (GameObject)Instantiate(gameObject.GetComponent<Tank>().Shell,Canon.transform.position, Canon.transform.rotation);
         shell.GetComponent<Shell>().setPlayer(gameObject.GetComponent<Player>());
         shell.SetActive(true); 
         shell.GetComponent<Rigidbody>().AddForce(CanonBase.transform.forward * shell.GetComponent<Shell>().Velocity); // If inactive : velocity lost after activation
         NetworkServer.Spawn(shell);
     }

As you can guess, this is the "Network" implementation of the code. Which works perfectly fine... as long as you are the server...

When you're a Client, and you shoot AND jump (yes you can jump) as the same time, something happen that I dont understand : Because the force of the jump can be, in some case, bigger than the ejection force of the shell, the shell will go through the canon from the bottom side sometimes (cause the shell is not a solid but a trigger), the shell will hit the chassis (base) of the tank who shot it. It can be manageable as long as I can detect the fact than the shell is from the same player (and so, ignore the collision and continue its course) but, IN SOME RANDOM CASES, the reference to the player is lost/never injected, and I get a "NullReferenceException" in this function

 void OnTriggerEnter(Collider collider){
         GameObject target = collider.gameObject;
         Debug.Log("Shell Player : " + this.player.ToString()); // NullReferenceException
         if(!target.name.Equals("Canon") && !target.name.Equals("Weight")){
             if(target.GetComponent<TankPart>() != null){
                 Debug.Log("Target Id : " + target.GetComponent<TankPart>().Player.Id);
                 Debug.Log("Shell Id : " + this.player.Id);
                 if(!target.GetComponent<TankPart>().Player.Id.Equals(player.Id)){
                     target.GetComponent<TankPart>().takeDamages(gameObject);
                 }
             }
             Debug.Log(collider.ToString());
             NetworkServer.Destroy(gameObject);
         }

To me it's like the physic continue his calculation at the same time the rest of it do the execution of the first code, and the trigger is detected between the 1st and 2nd line of the CmdShoot() function (before the reference is sent). Also, I said it looks like it's sometime never injected, because sometime I dont jump, the shell goes, and when it hits the ground, I got a "NullReferenceException" too. Maybe it's the same case, it's just hard to tell.

The call of the CmdShoot() was at first in the Update() loop, so I've also tried to put it into the FixedUpdate() loop (after all, it's a physical action, so, it makes sens to put it here after all) but the issue came anyway (maybe more rarely. Hard to tell, again). In my latest test, the Issue is encounter 1/28 shoot. Which is a lot.

I've also my console which is full of

"Did not find target for sync message for 175 UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()"

I dont think it's related, but I think it's good to notify that to you all.

I'm pretty desperate, I'm working on this issue since last Sunday and I can't find any long term solution. I hope someone here can help me.

I hope my post is clear. English is not my first language :/.

Cheers everyone. Thank in advance.

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

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

unity networking start host with autocreate 0 Answers

How to count score on all players from non player object 0 Answers

How do I get the true position (Vector 3) from a Network Transform Component(Unet)? 0 Answers

Day/Night cycle turns black at night and how to Serialize it rightly? 0 Answers

Photon Networking - How to move all players from game scene to current room scene? 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