- Home /
Unassigned Reference Exception - Even though it is assigned C#
I specifically assign the parent GameObject here:
GameObject projectile = (GameObject)Instantiate(bullet, spawnPosition, transform.rotation);
projectile.SendMessage("CmdSetParent", gameObject);
NetworkServer.Spawn(projectile);
The GameObject variable parent does NOT mean the actual physical parent, but rather the object that instantiated the bullet.
The problem is, when running Debug.Log(parent)
in update, I get the error. Even if this only happens in the first few shots, all shots after that should have eventually had the variable assigned right? I will note that this ONLY happens on the client. Host does not have this problem, and the parent is assigned properly.
Answer by Zetya96 · Dec 06, 2017 at 10:07 AM
Hi,
The cmdSetParent is only called on the server, you have to assign the "parent" on the clients too. Maybe, make the "parent" variable [syncvar], then if its changed value on the server, it'll be synchronized on the clients too.
Thanks for this. I ended up scrapping my physics based shooting system in exchange for raycasts with a particle system to simulate the bullets.
Although what you've said is true and would have solved that part of my situation, the system was doomed to fail as I could not set the parent before it ran its start or awake method. This problem would not occur if unity supported traditional constructors.
Your answer
Follow this Question
Related Questions
Unity 5 UNet Spawn as Child. 1 Answer
Is Unity 2018.1.5f1 compatible with .NET Standard? 0 Answers
Multiple Cars not working 1 Answer
Problem in build but no problem in playmode - DDS/netmodule 0 Answers
Distribute terrain in zones 3 Answers