Search Unity

UNET add NetworkTransformChild through script

Discussion in 'UNet' started by Go2ready, Mar 8, 2016.

  1. Go2ready

    Go2ready

    Joined:
    May 8, 2015
    Posts:
    29
    Hello there!

    I am wondering can I add NetworkTransformChild component to my game object by script? I have child object that is generated for parent on the fly, when I try to do this, more specifically

    Code (CSharp):
    1. var childT = this.AddComponent<NetworkTransformChild>();
    2. childT.target = ...

    the line 134(if I remember correctly) in the Awake function call in NetworkTransformChild.cs, throws NullPointException.

    This is expected if NetworkTransformChild.cs use the target field in the Awake function. Despite that I intended to setup the target field on the next line after creation. Is there any workaround without implementing my own child object synchroniser? Or can I peak through how the NetworkTransformChild is implemented?

    Here is the doc
    http://docs.unity3d.com/Manual/class-NetworkTransformChild.html

    With thanks!
    Richard
     
    Last edited: Mar 8, 2016
  2. CalebC

    CalebC

    Joined:
    Jul 15, 2012
    Posts:
    2
    I just ran into this this morning. Not sure if I'm blocked yet, but bumping this thread anyway

    NullReferenceException: Object reference not set to an instance of an object
    UnityEngine.Networking.NetworkTransformChild.Awake () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkTranformChild.cs:135)
    UnityEngine.GameObject:AddComponent()
    ObjectTheory.Avatar:Start() (at Assets/ObjectTheory/Avatar/v2/Scripts/Avatar.cs:168)

    This is the output immediately after creating a NetworkTransformChild in code:

    NetworkTransformChild networkTransformChild = calibratedSpace.AddComponent<NetworkTransformChild>();
    networkTransformChild.target = transform;
     
  3. jiezhou0731

    jiezhou0731

    Joined:
    May 17, 2016
    Posts:
    8
    Do you find the solution? I ran into the same problem.
     
  4. jiezhou0731

    jiezhou0731

    Joined:
    May 17, 2016
    Posts:
    8
    The NetworkTransformChild doesn't work, if it is created on the fly.
     
  5. DanielDollerup

    DanielDollerup

    Joined:
    Sep 18, 2013
    Posts:
    8
    Yeah, I just got this issue as well, pretty sure it's a bug. It wouldn't have a public target property, it it was not meant to be used on runtime. Or that's what I'm guessing.
     
  6. Donderblauw

    Donderblauw

    Joined:
    Dec 15, 2016
    Posts:
    1
  7. f4f4fd

    f4f4fd

    Joined:
    Feb 18, 2017
    Posts:
    3
    There is definitely a bug in NetworkTransformChild, but I have a workaround.

    The Bug:
    In the awake function of NetworkTransformChild (see source), the localPosition and localPosition attributes of m_Target are being accessed even though m_Target is null. When you call AddComponent<NetworkTransformChild>(), awake immediately starts executing, before you had a chance to set the "target" attribute.

    The Workaround:
    You can prevent awake from running automatically by disabling the gameobject before you add the component, and then re-enable it after you had a chance to set "target":
    Code (CSharp):
    1. this.gameObject.setActive(false);
    2. NetworkTransformChild childT = this.gameObject.AddComponent<NetworkTransformChild>();
    3. childT.target = ...
    4. this.gameObject.setActive(true);